zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: aliases not getting expanded inside functions?
Date: Sat, 11 Jan 2003 18:40:19 +0000	[thread overview]
Message-ID: <1030111184020.ZM11764@candle.brasslantern.com> (raw)
In-Reply-To: <15903.36155.716460.639226@fisica.ufpr.br>

On Jan 11,  1:19am, Carlos Carvalho wrote:
>
> Sorry I couldn't follow up earlier :-( Anyway the below told me how to
> pass parameters by name to a function so that it changes the values.
> Good!

That isn't the only way to do that, and not even the most readable in my
opinion.  For scalars, each of the following has the same effects:

	: ${(P)1::=$2}
	eval $1='$2'
	typeset -g $1=$2

For arrays:

	eval $1='( $3 $2 )'
	set -A $1 $3 $2

The array form ${(P)=1::=$3 $2} is almost but not quite the same, because
field splitting is applied *after* expanding $3 and $2 in that case, even
if they're quoted, so you can't preserve embedded whitespace.

> Bart Schaefer (schaefer@brasslantern.com) wrote on 6 January 2003 12:54:
>  >Nearly as often, the right thing is instead to ask the list how to solve
>  >problem X, because there's a better solution than Y.
> 
> Agreed, so here's the story, with two questions. I read a csv file
> that comes from a spreadsheet and need to split the fields to
> different variables. Instead of doing the full parsing of the data
> line by hand, it's easier to have zsh do the split:
> 
> fields=( ${(s:;:)dataline} )

How did you get the data into $dataline in the first place?

Rather than:

    while read dataline
    do
      fields=( "${(@s:;:)dataline}" )	# Answers your other question
      rate=$fields[1] capital=$fields[2] etc.
      # manipulate $rate $capital and so on ...
    done

You can simply do:

    while IFS=';' read rate capital etc.
    do
      # manipulate $rate $capital and so on ...
    done

> rate_prev=$rate capital_prev=$capital etc.
> 
> Instead of copying manually I'd like to do
> 
> rate_prev=$fields_prev[1] capital_prev=$fields_prev[2] etc.
> 
> only once, and then just do fields_prev=( $fields ) whenever I have to
> copy the values.

A much better way to do this is to use two associative arrays:

    typeset -A fields fields_prev
    while IFS=';' noglob read fields[rate] fields[capital] etc.
    do
      fields_prev=( ${(kv)fields} )	# Save all keys and values
      # manipulate $fields[rate] $fields[capital] and so on ...
    done

The "noglob" above is to avoid having to quote all the square brackets
in the "read" command (otherwise they'd be treated as file patterns).

> I mentioned some variant of a loop like
> 
> for ((i=1; i<= num_fields; i++)) {
>     : ${(P)${fields_prev[i]}::=${(P)${fields[i]}}}
> }
> 
> However this doesn't work because I cannot assign to the individual
> variables (ex. capital=$((capital+interest)) ) without losing the
> connection with the fields array.

If you're unwilling to use $fields[capital] everywhere -- that is, if
you insist on being able to write $capital in some cases -- then there
is no solution I can suggest.  However, if it's OK to write e.g.

    fields[capital]=$((fields[capital]+fields[interest]))

or, more succinctly,

    (( fields[capital] += fields[interest] ))

then there is no connection to worry about being lost.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


  reply	other threads:[~2003-01-11 18:41 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-03 15:31 Carlos Carvalho
2003-01-03 15:39 ` Zefram
2003-01-03 15:45 ` Phil Pennock
2003-01-03 17:24   ` Carlos Carvalho
2003-01-03 17:44     ` Phil Pennock
2003-01-03 18:10       ` Carlos Carvalho
2003-01-03 18:54         ` Zefram
2003-01-06  8:29           ` Carlos Carvalho
2003-01-06 12:54             ` Bart Schaefer
2003-01-11  3:19               ` Carlos Carvalho
2003-01-11 18:40                 ` Bart Schaefer [this message]
2003-01-13 17:59                   ` Carlos Carvalho
2003-01-13 18:48                     ` Bart Schaefer
2003-01-13 21:40                       ` Carlos Carvalho
2003-01-14  5:10                         ` Bart Schaefer
2003-01-14 11:44                           ` Roman Neuhauser

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1030111184020.ZM11764@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).