zsh-users
 help / color / mirror / code / Atom feed
* Apparent inconsistency in f/z expansion flags behavior
@ 2016-12-18 23:02 Pablo Lalloni
  2016-12-18 23:29 ` Jim
  2016-12-19  2:08 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Pablo Lalloni @ 2016-12-18 23:02 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 722 bytes --]

Hello there!

I'm seeing an unexpected (to me) difference in the behavior of these flags.

Say you do something like:

words=(${(z)$(</proc/meminfo)})

Which set words with the array of all the words in the file and that's
great.

Then I try the f flag in place of z, expecting to get an array with the
file's lines:

lines=(${(f)$(</proc/meminfo)})

But then I get an array with just one string containing all the lines
concatenated (no NLs).

So... Is this expected? What I need to do to get the array of lines?

Note that if you split the last assignment in 2 steps, it works as expected:

lines=$(</proc/meminfo)
lines=(${(f)lines})

Which is ok, but of course anybody would want to do it in one step, right?

Cheers!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Apparent inconsistency in f/z expansion flags behavior
  2016-12-18 23:02 Apparent inconsistency in f/z expansion flags behavior Pablo Lalloni
@ 2016-12-18 23:29 ` Jim
  2016-12-19  2:08 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Jim @ 2016-12-18 23:29 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1000 bytes --]

On Sun, Dec 18, 2016 at 5:02 PM, Pablo Lalloni <plalloni@gmail.com> wrote:

> Hello there!
>
> I'm seeing an unexpected (to me) difference in the behavior of these flags.
>
> Say you do something like:
>
> words=(${(z)$(</proc/meminfo)})
>
> Which set words with the array of all the words in the file and that's
> great.
>
> Then I try the f flag in place of z, expecting to get an array with the
> file's lines:
>
> lines=(${(f)$(</proc/meminfo)})
>
> But then I get an array with just one string containing all the lines
> concatenated (no NLs).
>
> So... Is this expected? What I need to do to get the array of lines?
>
> Note that if you split the last assignment in 2 steps, it works as
> expected:
>
> lines=$(</proc/meminfo)
> lines=(${(f)lines})
>
> Which is ok, but of course anybody would want to do it in one step, right?
>
> Cheers!
>

lines=("${(f)$(</proc/meminfo)}")

Should do what you want. You need the quotes. It took me a
while to get that into my own head.

Hope it helps,

Jim

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Apparent inconsistency in f/z expansion flags behavior
  2016-12-18 23:02 Apparent inconsistency in f/z expansion flags behavior Pablo Lalloni
  2016-12-18 23:29 ` Jim
@ 2016-12-19  2:08 ` Bart Schaefer
  2016-12-19 15:02   ` Pablo Lalloni
  1 sibling, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2016-12-19  2:08 UTC (permalink / raw)
  To: zsh-users

On Dec 18,  8:02pm, Pablo Lalloni wrote:
} 
} words=(${(z)$(</proc/meminfo)})
} 
} Which set words with the array of all the words in the file and that's
} great.
} 
} lines=(${(f)$(</proc/meminfo)})
} 
} But then I get an array with just one string containing all the lines
} concatenated (no NLs).

That's happening because $(</proc/meminfo) has replaced all the NLs with
spaces before (z) or (f) begin working.  (z) doesn't care because it
splits on shell-syntax whitespace, but (f) has nothing to split on.

To get what you want, you have to quote the $(...) substitution so the
NLs are preserved:

    lines=(${(f)"$(</proc/meminfo)"})

That would be "more correct" in the (z) use as well, just in case the
line breaks were e.g. inside quoted strings that would change the shell
parse.  (Not an issue with /proc/meminfo, of course, but in general.)

} Note that if you split the last assignment in 2 steps, it works as expected:
} 
} lines=$(</proc/meminfo)
} lines=(${(f)lines})

There you've done first an assignment to a scalar, which preserves the
NLs as if $(...) were quoted.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Apparent inconsistency in f/z expansion flags behavior
  2016-12-19  2:08 ` Bart Schaefer
@ 2016-12-19 15:02   ` Pablo Lalloni
  0 siblings, 0 replies; 4+ messages in thread
From: Pablo Lalloni @ 2016-12-19 15:02 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1337 bytes --]

Thanks for your responses guys.

Both variations works as you said.

Better yet, I understand why.

Cheers!

On Sun, Dec 18, 2016 at 11:08 PM, Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Dec 18,  8:02pm, Pablo Lalloni wrote:
> }
> } words=(${(z)$(</proc/meminfo)})
> }
> } Which set words with the array of all the words in the file and that's
> } great.
> }
> } lines=(${(f)$(</proc/meminfo)})
> }
> } But then I get an array with just one string containing all the lines
> } concatenated (no NLs).
>
> That's happening because $(</proc/meminfo) has replaced all the NLs with
> spaces before (z) or (f) begin working.  (z) doesn't care because it
> splits on shell-syntax whitespace, but (f) has nothing to split on.
>
> To get what you want, you have to quote the $(...) substitution so the
> NLs are preserved:
>
>     lines=(${(f)"$(</proc/meminfo)"})
>
> That would be "more correct" in the (z) use as well, just in case the
> line breaks were e.g. inside quoted strings that would change the shell
> parse.  (Not an issue with /proc/meminfo, of course, but in general.)
>
> } Note that if you split the last assignment in 2 steps, it works as
> expected:
> }
> } lines=$(</proc/meminfo)
> } lines=(${(f)lines})
>
> There you've done first an assignment to a scalar, which preserves the
> NLs as if $(...) were quoted.
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-12-19 15:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-18 23:02 Apparent inconsistency in f/z expansion flags behavior Pablo Lalloni
2016-12-18 23:29 ` Jim
2016-12-19  2:08 ` Bart Schaefer
2016-12-19 15:02   ` Pablo Lalloni

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).