zsh-users
 help / color / mirror / code / Atom feed
* Can this be done with an array parameter?
@ 2004-02-19 16:46 DervishD
  2004-02-19 17:00 ` Peter Stephenson
  2004-02-19 17:16 ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: DervishD @ 2004-02-19 16:46 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

    I need to process some file contents line-by-line, and I was
going to do it by using a loop, like this:

    cat filename | while read line
    do
        things to do with $line
    done

    Since some of the work would be better done with an array, I
changed this to:

    set -A info
    cat filename | while read line
    do
        info=($info $line)
    done

    This effectively store every line as a separate item in the array
parameter, which surprises me. Lets assume the file contents are:

    A line
    Another line
    Yet another line
    More lines below

    Then the loop will result in (just guessing...):

    info=( A line)
    info=(A line Another line)
    info=(A line Another line Yet another line)
    info=(A line Another line Yet another line More lines below)

    But instead, the 'line' seems to be added *quoted* :?? It's like
Zsh is reading my mind or so. Can anybody explain? As far as I knew,
the more correct way of doing the assignment in the loop, will be:

    index=$(($index+1))
    info[$index]="$line"

    considering 'index' value of '0' at start.

    And my original question: can the array be assigned one line of
the file per slot without the loop?

    Thanks a lot in advance :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Can this be done with an array parameter?
  2004-02-19 16:46 Can this be done with an array parameter? DervishD
@ 2004-02-19 17:00 ` Peter Stephenson
  2004-02-19 17:14   ` DervishD
  2004-02-19 17:16 ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2004-02-19 17:00 UTC (permalink / raw)
  To: Zsh users list

DervishD wrote:
>     And my original question: can the array be assigned one line of
> the file per slot without the loop?

array=(${(f)"$(<filename)"})

$(<filename)
  Substitute the file.
"..."
  The substitution is performed as a single command line argument.
${(f)...}
  Split the result into one element per line (a pseudo-parameter-substitution;
  they allow command substitutions, too).  (Sometimes you see an @ next to
  the f to tell it the result is an array; this is harmless but unnecessary
  in this case.)

There are lots of examples like this in the completion code, except most
are about fifty times more complicated.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: Can this be done with an array parameter?
  2004-02-19 17:00 ` Peter Stephenson
@ 2004-02-19 17:14   ` DervishD
  2004-02-19 17:24     ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: DervishD @ 2004-02-19 17:14 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh users list

    Hi Peter :)

 * Peter Stephenson <pws@csr.com> dixit:
> >     And my original question: can the array be assigned one line of
> > the file per slot without the loop?
> array=(${(f)"$(<filename)"})

    Yes :( I should have remembered this... A very intelligent, easy
an elegant solution. Thanks a lot for the help :))
 
> There are lots of examples like this in the completion code, except most
> are about fifty times more complicated.

    And its logical: I should have took a look at the completion
code, since it processes a lot of files. I just didn't thought of it,
sorry for the noise :((

    Anyway I'm still stunned by the first example I put, the
assignment to the array in the form array=($array $newitem) Why Zsh
quotes correctly? (I should define 'correctly' as 'the way I
wanted'...).

    Again, thanks for your answer :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Can this be done with an array parameter?
  2004-02-19 16:46 Can this be done with an array parameter? DervishD
  2004-02-19 17:00 ` Peter Stephenson
@ 2004-02-19 17:16 ` Bart Schaefer
  2004-02-19 17:27   ` DervishD
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2004-02-19 17:16 UTC (permalink / raw)
  To: Zsh Users

On Feb 19,  5:46pm, DervishD wrote:
}
}     cat filename | while read line
}     do
}         info=($info $line)
}     done
} 
}     This effectively store every line as a separate item in the array
} parameter, which surprises me.  [...]
} 
} Zsh is reading my mind or so. Can anybody explain?

I note that PWS didn't bother to explain. :-)

Unless the SH_WORD_SPLIT option is set, variable references like $info
and $line aren't exactly quoted, but they aren't split up at spaces
either.  $info is an array, so it acts (almost) like "${info[@]}", but
$line is a scalar, so it acts (almost) like "$line".

The "almost" part has to do with what happens to array elements or
variables whose value is the empty string, and unset variables.  They
disappear entirely from the expanded command line rather than remaining
there as a quoted empty string.


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

* Re: Can this be done with an array parameter?
  2004-02-19 17:14   ` DervishD
@ 2004-02-19 17:24     ` Peter Stephenson
  2004-02-19 17:41       ` DervishD
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2004-02-19 17:24 UTC (permalink / raw)
  To: Zsh users list

DervishD wrote:
>     Anyway I'm still stunned by the first example I put, the
> assignment to the array in the form array=($array $newitem) Why Zsh
> quotes correctly? (I should define 'correctly' as 'the way I
> wanted'...).

I presume because you're sensible enough not to have sh_word_split set.
You could still do it as

array=("${array[@}" "$newitem")

which is what you'd have to do in bash.

By the way, in 4.1 you can now do

array+=($newitem)

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: Can this be done with an array parameter?
  2004-02-19 17:16 ` Bart Schaefer
@ 2004-02-19 17:27   ` DervishD
  0 siblings, 0 replies; 7+ messages in thread
From: DervishD @ 2004-02-19 17:27 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> } Zsh is reading my mind or so. Can anybody explain?
> I note that PWS didn't bother to explain. :-)

    Well, he gave me the solution for the assignment without a loop ;)
 
> Unless the SH_WORD_SPLIT option is set, variable references like $info
> and $line aren't exactly quoted, but they aren't split up at spaces
> either.  $info is an array, so it acts (almost) like "${info[@]}", but
> $line is a scalar, so it acts (almost) like "$line".

    Oh, yes, I forgot about that. I simply assumed that, since array
values are space separated by default, the contents of the variables
were too. I didn't remember about word splitting O:)

    Thanks a lot for the explanation, Bart ;)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Can this be done with an array parameter?
  2004-02-19 17:24     ` Peter Stephenson
@ 2004-02-19 17:41       ` DervishD
  0 siblings, 0 replies; 7+ messages in thread
From: DervishD @ 2004-02-19 17:41 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh users list

    Hi Peter :)

 * Peter Stephenson <pws@csr.com> dixit:
> DervishD wrote:
> >     Anyway I'm still stunned by the first example I put, the
> > assignment to the array in the form array=($array $newitem) Why Zsh
> > quotes correctly? (I should define 'correctly' as 'the way I
> > wanted'...).
> I presume because you're sensible enough not to have sh_word_split set.

    When I first used Zsh, I had the option set, since I was more
familiar with that behaviour, but I unsetted it soon ;)

> array=("${array[@}" "$newitem")
> which is what you'd have to do in bash.

    And that was the notation I spected ;) I just didn't remember how
Zsh does word splitting ;)
 
> By the way, in 4.1 you can now do
> array+=($newitem)

    Nice!

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

end of thread, other threads:[~2004-02-19 17:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-19 16:46 Can this be done with an array parameter? DervishD
2004-02-19 17:00 ` Peter Stephenson
2004-02-19 17:14   ` DervishD
2004-02-19 17:24     ` Peter Stephenson
2004-02-19 17:41       ` DervishD
2004-02-19 17:16 ` Bart Schaefer
2004-02-19 17:27   ` DervishD

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