zsh-users
 help / color / mirror / code / Atom feed
* Confused about splitting
@ 2019-03-31  9:06 Anssi Saari
  2019-03-31 12:58 ` Daniel Shahaf
  2019-03-31 13:12 ` Daniel Shahaf
  0 siblings, 2 replies; 5+ messages in thread
From: Anssi Saari @ 2019-03-31  9:06 UTC (permalink / raw)
  To: zsh-users


I have a simple script which uses awk to assign and split a line from a
file to a zsh array. Simplified, just this:

foofunc () {
	if [[ $# -ge 1 ]]
	then
		if=$1 
	else
		if=eno1
	fi
	first=(`awk /$if/ /proc/net/dev`)

	echo $first
	echo length of first is $#first
}

Obvious and works, it prints out currently with parameter eno1 this:

eno1: 44977581223 33469291 0 139 0 0 0 57718 12574512832 21508919 0 0 0 0 0 0
length of first is 17

/proc/net/dev currently contains
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo: 990081491  293912    0    0    0     0          0         0 990081491  293912    0    0    0     0       0          0
  eno1: 44978021588 33472121    0  139    0     0          0     57964 12575022205 21512486    0    0    0     0       0          0

But wanting to not call awk I ran into trouble.

If I set shwordsplit, no problem:

foofunc2 () {
	if [[ $# -ge 1 ]]
	then
		if=$1 
	else
		if=eno1
	fi
	setopt shwordsplit
	while read g
	do
	    if [[ $g =~ $if ]]
	    then
		first=($g)
		break
	    fi
	done  < /proc/net/dev

	echo $first
	echo length of first is $#first
}

But what if I don't want to set shwordsplit? I came up with

foofunc3 () {
	if [[ $# -ge 1 ]]
	then
		if=$1 
	else
		if=eno1
	fi
	while read g
	do
	    if [[ $g =~ $if ]]
	    then
		first=${=g}
		break
	    fi
	done < /proc/net/dev

	echo $first
	echo length of first is $#first
}

But this splits each character into an array element. I don't understand
this behavior at all. As I understand it, = in the assignment to $first
is supposed to turn shwordsplit on temporarily but it really doesn't
seem to. IFS is not set so field separator should be default, meaning
whitespace.

This is with zsh 5.3.1 on Debian.


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

* Re: Confused about splitting
  2019-03-31  9:06 Confused about splitting Anssi Saari
@ 2019-03-31 12:58 ` Daniel Shahaf
  2019-03-31 17:56   ` Anssi Saari
  2019-03-31 13:12 ` Daniel Shahaf
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Shahaf @ 2019-03-31 12:58 UTC (permalink / raw)
  To: Anssi Saari; +Cc: zsh-users

Anssi Saari wrote on Sun, Mar 31, 2019 at 12:06:16 +0300:
> foofunc2 () {
> 	setopt shwordsplit

This should have been «setopt localoptions shwordsplit».

⋮
> }

> But what if I don't want to set shwordsplit? I came up with
> 
> foofunc3 () {
> 	if [[ $# -ge 1 ]]
> 	then
> 		if=$1 
> 	else
> 		if=eno1
> 	fi
> 	while read g
> 	do
> 	    if [[ $g =~ $if ]]
> 	    then
> 		first=${=g}
> 		break
> 	    fi
> 	done < /proc/net/dev
> 
> 	echo $first
> 	echo length of first is $#first
> }
> 
> But this splits each character into an array element. I don't understand
> this behavior at all. As I understand it, = in the assignment to $first
> is supposed to turn shwordsplit on temporarily but it really doesn't
> seem to. IFS is not set so field separator should be default, meaning
> whitespace.

«${=g}» does split the value of the parameter 'g'; you can see that with
«printf '[%s]' ${=g}».  Then, that intermediate result — an array of words — is
assigned to the parameter 'first' scalarly, so the value of $first is set to be
the words of the array joined by spaces as a single string.  That's why «echo
$first» has the right output.  The «echo $#first» line prints the length of the
string "$first" in characters, because that's what the «$#foo» syntax does when
foo is a scalar.

The fix is to add back the parentheses to make the assignment an array
assignment: «first=( ${=g} )».

Furthermore, note that if $first had been an array of one-character strings,
the output of the first 'echo' would have been incorrect: compare «echo $foo»
(echos a string) and «echo ${(s::)foo}» (echos an array of one-character
strings).

Cheers,

Daniel

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

* Re: Confused about splitting
  2019-03-31  9:06 Confused about splitting Anssi Saari
  2019-03-31 12:58 ` Daniel Shahaf
@ 2019-03-31 13:12 ` Daniel Shahaf
  2019-04-01  9:07   ` Anssi Saari
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Shahaf @ 2019-03-31 13:12 UTC (permalink / raw)
  To: Anssi Saari; +Cc: zsh-users

Anssi Saari wrote on Sun, Mar 31, 2019 at 12:06:16 +0300:
> foofunc3 () {
> 	if [[ $# -ge 1 ]]
> 	then
> 		if=$1 
> 	else
> 		if=eno1
> 	fi
> 	while read g
> 	do
> 	    if [[ $g =~ $if ]]
> 	    then
> 		first=${=g}
> 		break
> 	    fi
> 	done < /proc/net/dev
> 
> 	echo $first
> 	echo length of first is $#first
> }

Here's another solution:

     1	% () {
     2	    : ${1:=enp2s0}
     3	    local -a net_dev_lines=( "${(@f)"$(</proc/net/dev)"}" )
     4	    local line=${${(M)net_dev_lines:#*$1*}[1]}
     5	    print -r -- $=line
     6	    print -r -- ${#${=line}}
     7	  }
     8	17
     9	% 

Line 2 sets $1 to "enp2s0" if it's not already set.

Line 3 slurps the target file (the «$(<…)») and splits it into lines (the «${(f)}»).

Line 4 greps it (that's the :# and the (M) together) and takes the first result
(that's the outer «${…[1]}» construct).

Line 6 splits it, takes the length of the intermediate array, and prints that.

Cheers,

Daniel

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

* Re: Confused about splitting
  2019-03-31 12:58 ` Daniel Shahaf
@ 2019-03-31 17:56   ` Anssi Saari
  0 siblings, 0 replies; 5+ messages in thread
From: Anssi Saari @ 2019-03-31 17:56 UTC (permalink / raw)
  To: zsh-users

Daniel Shahaf <d.s@daniel.shahaf.name> writes:

> «${=g}» does split the value of the parameter 'g'; you can see that with
> «printf '[%s]' ${=g}».  Then, that intermediate result — an array of words — is
> assigned to the parameter 'first' scalarly, so the value of $first is set to be
> the words of the array joined by spaces as a single string.  That's why «echo
> $first» has the right output.  The «echo $#first» line prints the length of the
> string "$first" in characters, because that's what the «$#foo» syntax does when
> foo is a scalar.

This mistaken idea came from the fact $first[1] is just e with
foofunc3. $first[2] is n, $first[3] is o and so on. For some reason I
thought scalars don't have that but they do.

> The fix is to add back the parentheses to make the assignment an array
> assignment: «first=( ${=g} )».

Thanks.


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

* Re: Confused about splitting
  2019-03-31 13:12 ` Daniel Shahaf
@ 2019-04-01  9:07   ` Anssi Saari
  0 siblings, 0 replies; 5+ messages in thread
From: Anssi Saari @ 2019-04-01  9:07 UTC (permalink / raw)
  To: zsh-users

Daniel Shahaf <d.s@daniel.shahaf.name> writes:

>      1	% () {
>      2	    : ${1:=enp2s0}
>      3	    local -a net_dev_lines=( "${(@f)"$(</proc/net/dev)"}" )
>      4	    local line=${${(M)net_dev_lines:#*$1*}[1]}
>      5	    print -r -- $=line
>      6	    print -r -- ${#${=line}}
>      7	  }
>      8	17
>      9	% 

Thanks but that's mostly the kind of syntax I wish to avoid.


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

end of thread, other threads:[~2019-04-01  9:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-31  9:06 Confused about splitting Anssi Saari
2019-03-31 12:58 ` Daniel Shahaf
2019-03-31 17:56   ` Anssi Saari
2019-03-31 13:12 ` Daniel Shahaf
2019-04-01  9:07   ` Anssi Saari

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).