zsh-users
 help / color / mirror / code / Atom feed
* aliases not getting expanded inside functions?
@ 2003-01-03 15:31 Carlos Carvalho
  2003-01-03 15:39 ` Zefram
  2003-01-03 15:45 ` Phil Pennock
  0 siblings, 2 replies; 16+ messages in thread
From: Carlos Carvalho @ 2003-01-03 15:31 UTC (permalink / raw)
  To: zsh-users

Hi users,

First, thanks Bart for the info on the C preprocessor bug.

Now I'm trying to do something like

% alias -g foo='bar'
% escr() {print $foo}

I'd expect that since the alias is global, comes before the definition
of the function, and aliases are expanded when a function is read,
that the function would be in effect {print $bar}, but it isn't:

% which escr
escr () {
        print $foo
}

The "problem" is with the $, because if I write {print foo} instead of
{print $foo} in the definition the function appears as {print bar}, as
I expected. Is this correct? If so, is there a way to achieve this
substitution? This is in a script, not interactive.

Thanks in advance.


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

* Re: aliases not getting expanded inside functions?
  2003-01-03 15:31 aliases not getting expanded inside functions? Carlos Carvalho
@ 2003-01-03 15:39 ` Zefram
  2003-01-03 15:45 ` Phil Pennock
  1 sibling, 0 replies; 16+ messages in thread
From: Zefram @ 2003-01-03 15:39 UTC (permalink / raw)
  To: Carlos Carvalho; +Cc: zsh-users

Carlos Carvalho wrote:
>I'd expect that since the alias is global, comes before the definition
>of the function, and aliases are expanded when a function is read,
>that the function would be in effect {print $bar}, but it isn't:

Global aliases don't do that.

-zefram


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

* Re: aliases not getting expanded inside functions?
  2003-01-03 15:31 aliases not getting expanded inside functions? Carlos Carvalho
  2003-01-03 15:39 ` Zefram
@ 2003-01-03 15:45 ` Phil Pennock
  2003-01-03 17:24   ` Carlos Carvalho
  1 sibling, 1 reply; 16+ messages in thread
From: Phil Pennock @ 2003-01-03 15:45 UTC (permalink / raw)
  To: zsh-users

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

On 2003-01-03 at 13:31 -0200, Carlos Carvalho wrote:
> Now I'm trying to do something like
> 
> % alias -g foo='bar'
> % escr() {print $foo}

% bar='Fred Bloggs'
% foo='bar'
% function escr { print ${(P)foo} }
% escr
Fred Bloggs
%

>                              If so, is there a way to achieve this
> substitution? This is in a script, not interactive.

Is the above sufficient?

You might wish to read zshexpn(1), since there are many other weird and
wonderful ways in which zsh can pervert your data on demand.
-- 
"We've got a patent on the conquering of a country through the use of force.
 We believe in world peace through extortionate license fees." -- Andy Forster

[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]

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

* Re: aliases not getting expanded inside functions?
  2003-01-03 15:45 ` Phil Pennock
@ 2003-01-03 17:24   ` Carlos Carvalho
  2003-01-03 17:44     ` Phil Pennock
  0 siblings, 1 reply; 16+ messages in thread
From: Carlos Carvalho @ 2003-01-03 17:24 UTC (permalink / raw)
  To: zsh-users

Phil Pennock (Phil.Pennock@globnix.org) wrote on 3 January 2003 16:45:
 >% bar='Fred Bloggs'
 >% foo='bar'
 >% function escr { print ${(P)foo} }
 >% escr
 >Fred Bloggs
 >
 >Is the above sufficient?

Unfortunately no, because I don't need it only for printing, but for
referencing as well. I have an array and I'd like to access the
elements by more meaningful names, so I tried to do

% alias -g guess_what='array[1]', etc.

Your suggestion doesn't work however when an assignment has to be made
to guess_what.

 >You might wish to read zshexpn(1), since there are many other weird and
 >wonderful ways in which zsh can pervert your data on demand.

I did but didn't see this possibility. In fact I still don't see...
What's the relation between recognizing the same escape sequences as
print and making a substitution? I'd be grateful for an explanation.


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

* Re: aliases not getting expanded inside functions?
  2003-01-03 17:24   ` Carlos Carvalho
@ 2003-01-03 17:44     ` Phil Pennock
  2003-01-03 18:10       ` Carlos Carvalho
  0 siblings, 1 reply; 16+ messages in thread
From: Phil Pennock @ 2003-01-03 17:44 UTC (permalink / raw)
  To: zsh-users

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

On 2003-01-03 at 15:24 -0200, Carlos Carvalho wrote:
> Phil Pennock (Phil.Pennock@globnix.org) wrote on 3 January 2003 16:45:
>  >% bar='Fred Bloggs'
>  >% foo='bar'
>  >% function escr { print ${(P)foo} }
>  >% escr
>  >Fred Bloggs
>  >
>  >Is the above sufficient?
> 
> Unfortunately no, because I don't need it only for printing, but for
> referencing as well. I have an array and I'd like to access the
> elements by more meaningful names, so I tried to do
> 
> % alias -g guess_what='array[1]', etc.
> 
> Your suggestion doesn't work however when an assignment has to be made
> to guess_what.
> 
>  >You might wish to read zshexpn(1), since there are many other weird and
>  >wonderful ways in which zsh can pervert your data on demand.
> 
> I did but didn't see this possibility. In fact I still don't see...
> What's the relation between recognizing the same escape sequences as
> print and making a substitution? I'd be grateful for an explanation.

print is merely what was used for showing the results.  The variable
substitution does not rely upon print.

% set -A array alpha beta gamma delta epsilon zeta
% guess_what='array[3]'
% echo ${(P)guess_what}
gamma
% guess_what='array[4]'
% echo ${(P)guess_what}
delta
% guess_what='array[1]'
% for i in $(perl -le "print foreach split //, '${(P)guess_what}'") ; echo $i
a
l
p
h
a
%

My suggestion appears to work, independent of echo/print.  :^)

I think that you're confusing "print -P", which recognises the same
escape sequences as are used in prompt substitution, with the parameter
expansion flag 'P', which is described in zshexpn(1).
-- 
"We've got a patent on the conquering of a country through the use of force.
 We believe in world peace through extortionate license fees." -- Andy Forster

[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]

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

* Re: aliases not getting expanded inside functions?
  2003-01-03 17:44     ` Phil Pennock
@ 2003-01-03 18:10       ` Carlos Carvalho
  2003-01-03 18:54         ` Zefram
  0 siblings, 1 reply; 16+ messages in thread
From: Carlos Carvalho @ 2003-01-03 18:10 UTC (permalink / raw)
  To: zsh-users

Phil Pennock (Phil.Pennock@globnix.org) wrote on 3 January 2003 18:44:
 >print is merely what was used for showing the results.  The variable
 >substitution does not rely upon print.

I agree, I just used print as an example of reading the value, not
setting it.

 >% set -A array alpha beta gamma delta epsilon zeta
 >% guess_what='array[3]'
 >% echo ${(P)guess_what}
 >gamma
 >% guess_what='array[4]'
 >% echo ${(P)guess_what}
 >delta

It doesn't work if you try to do guess_what=foobar, meaning
array[4]=foobar.

 >% guess_what='array[1]'
 >% for i in $(perl -le "print foreach split //, '${(P)guess_what}'") ; echo $i
 >a
 >l
 >p
 >h
 >a
 >%

Nice :-) I should have learned perl long since. In fact probably the
script I'm doing should be written in perl...

 >I think that you're confusing "print -P", which recognises the same
 >escape sequences as are used in prompt substitution, with the parameter
 >expansion flag 'P', which is described in zshexpn(1).

The manual says "Recognize the same escape sequences as the print
builtin in string arguments to subsequent flags." So there should be a
relation.


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

* Re: aliases not getting expanded inside functions?
  2003-01-03 18:10       ` Carlos Carvalho
@ 2003-01-03 18:54         ` Zefram
  2003-01-06  8:29           ` Carlos Carvalho
  0 siblings, 1 reply; 16+ messages in thread
From: Zefram @ 2003-01-03 18:54 UTC (permalink / raw)
  To: Carlos Carvalho; +Cc: zsh-users

Carlos Carvalho wrote:
>The manual says "Recognize the same escape sequences as the print
>builtin in string arguments to subsequent flags." So there should be a
>relation.

That's the "p" (*small* p) flag.  Phil was demonstrating the "P"
(*capital* P) parameter expansion flag, which zshexpn(1) describes thus:

#        P      This  forces  the value of the parameter name to be
#               interpreted as  a  further  parameter  name,  whose
#               value  will be used where appropriate. If used with
#               a nested parameter  or  command  substitution,  the
#               result of that will be taken as a parameter name in
#               the same way.  For example, if you  have  `foo=bar'
#               and `bar=baz', the strings ${(P)foo}, ${(P)${foo}},
#               and ${(P)$(echo bar)} will be expanded to `baz'.

To use this kind of parameter alias for setting, you can do

% foo=bar bar=baz
% echo ${(P)foo}
baz
% : ${(P)foo::=xxx}
% echo ${(P)foo}
xxx
% echo $foo $bar
bar xxx

which also works where foo is an array reference such as "array[3]".

-zefram


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

* Re: aliases not getting expanded inside functions?
  2003-01-03 18:54         ` Zefram
@ 2003-01-06  8:29           ` Carlos Carvalho
  2003-01-06 12:54             ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Carlos Carvalho @ 2003-01-06  8:29 UTC (permalink / raw)
  To: zsh-users

Zefram (zefram@fysh.org) wrote on 3 January 2003 18:54:
 >That's the "p" (*small* p) flag.  Phil was demonstrating the "P"
 >(*capital* P) parameter expansion flag, which zshexpn(1) describes thus:
 >
 >#        P      This  forces  the value of the parameter name to be
 >#               interpreted as  a  further  parameter  name,  whose
 >#               value  will be used where appropriate. If used with
 >#               a nested parameter  or  command  substitution,  the
...

Oops... This didn't appear in my manual. After some digging I found
that I was reading the old manual, for zsh 3, when this didn't exist
yet. Now I've cleaned the remnants of the old installation and I can
see the current manuals. Impressive! I'm starting to use some of the
new features, but it's just the beginning :-)

 >To use this kind of parameter alias for setting, you can do
...
 >% : ${(P)foo::=xxx}
 >% echo ${(P)foo}
 >xxx
 >% echo $foo $bar
 >bar xxx
 >
 >which also works where foo is an array reference such as "array[3]".

Thanks, I think I understood now. I wouldn't call it the simplest
thing to find out... In my case I'd have to do

% a[1]=name1 a[2]=name2 etc.
% b[1]=b_name1 b[2]=b_name2 etc.

and then to do the transfer

for ((i=1; i< number-of-elements; i++)) {
	: ${(P)b[i]::=$a[i]}
}

However this is too much of a contortion to be useful... Is there a
way to do the whole assignment without a loop? Something like
${(P)b::=$a} or similar (yes, this one cannot work of course).

It seems that putting assignments like b_name1=$name1 b_name2=$name2
etc. in a function and simply calling it ends up working better. The
only advantage of using the loop above is that it's independent of the
number of elements, but I think this independence isn't worth the
trouble.

Thanks a lot to the developers in the list, the level of support is
amazing.


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

* Re: aliases not getting expanded inside functions?
  2003-01-06  8:29           ` Carlos Carvalho
@ 2003-01-06 12:54             ` Bart Schaefer
  2003-01-11  3:19               ` Carlos Carvalho
  0 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2003-01-06 12:54 UTC (permalink / raw)
  To: zsh-users

On Jan 6,  6:29am, Carlos Carvalho wrote:
}
} % a[1]=name1 a[2]=name2 etc.
} % b[1]=b_name1 b[2]=b_name2 etc.
} 
} and then to do the transfer
} 
} for ((i=1; i< number-of-elements; i++)) {
} 	: ${(P)b[i]::=$a[i]}
} }

I presume you mean ${(P)${b[i]}::=${(P)${a[i]}}}, but I can't figure out
_why_ you'd want to do that.

This sort of question often results from a sequence of events along the
lines of:  Problem X is encountered.  Solution Y is considered.  Attempt
to implement Y fails at detail Z.  The list is asked how to accomplish Z.

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.

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


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

* Re: aliases not getting expanded inside functions?
  2003-01-06 12:54             ` Bart Schaefer
@ 2003-01-11  3:19               ` Carlos Carvalho
  2003-01-11 18:40                 ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Carlos Carvalho @ 2003-01-11  3:19 UTC (permalink / raw)
  To: zsh-users

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!

Bart Schaefer (schaefer@brasslantern.com) wrote on 6 January 2003 12:54:
 >On Jan 6,  6:29am, Carlos Carvalho wrote:
 >}
 >} % a[1]=name1 a[2]=name2 etc.
 >} % b[1]=b_name1 b[2]=b_name2 etc.
 >} 
 >} and then to do the transfer
 >} 
 >} for ((i=1; i< number-of-elements; i++)) {
 >} 	: ${(P)b[i]::=$a[i]}
 >} }
 >
 >I presume you mean ${(P)${b[i]}::=${(P)${a[i]}}}

Yes, sorry, according to my example you're right. I just note that the
braces are not necessary in ${b[i]}, $b[i] works as well.

 >but I can't figure out
 >_why_ you'd want to do that.
 >
 >This sort of question often results from a sequence of events along the
 >lines of:  Problem X is encountered.  Solution Y is considered.  Attempt
 >to implement Y fails at detail Z.  The list is asked how to accomplish Z.
 >
 >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} )

Now comes the first question: when there are several consecutive
semicolons the fields end up empty. This is correct, the problem is
that zsh removes the empty fields from the assignment, and instead of
getting 15 fields with many empty I have only 6 (I mean ${#fields} is
less than the number of variables in $dataline). Is there a way to
avoid this? I vaguely recall an option to not remove empty values from
the line but didn't find it. I've worked around the situation by
filling consecutive semicolons with some characters before doing the
split, but it's a hack.

Next, it's necessary to deal with the fields by meaningful names, so I'm
doing

rate=$fields[1] capital=$fields[2] etc.

I modify the variables directly, not only through fields. Later on I
have to copy all these fields to other variables like

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. This way with a single assignment all variables are
copied to the *_prev counterparts. If this is not possible, I
mentioned some variant of a loop like

for ((i=1; i<= num_fields; i++)) {
    fields_prev[i]=$fields[i]
}

which Bart said should be ${(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 have any better ideas please speak up. As I said, I used the
trick above in another place to make a function change the value of a
parameter, so it's already been useful. Thanks for it.


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

* Re: aliases not getting expanded inside functions?
  2003-01-11  3:19               ` Carlos Carvalho
@ 2003-01-11 18:40                 ` Bart Schaefer
  2003-01-13 17:59                   ` Carlos Carvalho
  0 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2003-01-11 18:40 UTC (permalink / raw)
  To: zsh-users

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   


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

* Re: aliases not getting expanded inside functions?
  2003-01-11 18:40                 ` Bart Schaefer
@ 2003-01-13 17:59                   ` Carlos Carvalho
  2003-01-13 18:48                     ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Carlos Carvalho @ 2003-01-13 17:59 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer (schaefer@brasslantern.com) wrote on 11 January 2003 18:40:
 >	: ${(P)1::=$2}
 >	eval $1='$2'
 >	typeset -g $1=$2

Nice!

 >Rather than:
 >
 >    while read dataline
 >    do
 >      fields=( "${(@s:;:)dataline}" )	# Answers your other question

Doesn't work for me:

    fields=( "${(@s:;:)dataline}" )
    print LINE=$dataline
    for ((i=1; i <= 16; i++)) { print "fields[$i]=\"$fields[i]\"" }

gives

LINHA="BD CCE";"CCE";"210";"0";"1-254";33;7;1;;;;;;;"permit";
fields[1]=""BD CCE""
fields[2]=""CCE""
fields[3]=""210""
fields[4]=""0""
fields[5]=""1-254""
fields[6]="33"
fields[7]="7"
fields[8]="1"
fields[9]=""permit""         <--- wrong
fields[10]=""
fields[11]=""
fields[12]=""
fields[13]=""
fields[14]=""
fields[15]=""                <--- permit should be here
fields[16]=""

 >      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

Yes, I didn't do it because I had hope to one day do a block move
via arrays. Also, reading to a parameter first makes it easier to do
some pre-processing like quote removal, otherwise I have to do it
field by field after reading.

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

This makes the program more cumbersome to write and read because all
variables become quite long-named...


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

* Re: aliases not getting expanded inside functions?
  2003-01-13 17:59                   ` Carlos Carvalho
@ 2003-01-13 18:48                     ` Bart Schaefer
  2003-01-13 21:40                       ` Carlos Carvalho
  0 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2003-01-13 18:48 UTC (permalink / raw)
  To: zsh-users

On Jan 13,  3:59pm, Carlos Carvalho wrote:
}
}  >      fields=( "${(@s:;:)dataline}" )	# Answers your other question
} 
} Doesn't work for me:

Hrm.  I was sure I'd tried it, but on your input I get the same result
you do.  Well, there's always:

    IFS=';' eval 'fields=( $=dataline )'

However, it occurs to me that with dataline='...;";";...' -- that is, a
quoted semicolon in the input data -- there's no quick solution that's
going to do exactly what you need.

}  >    while IFS=';' read rate capital etc.
} 
} Yes, I didn't do it because I had hope to one day do a block move
} via arrays. Also, reading to a parameter first makes it easier to do
} some pre-processing like quote removal, otherwise I have to do it
} field by field after reading.

[...]

}  >    fields[capital]=$((fields[capital]+fields[interest]))
} 
} This makes the program more cumbersome to write and read because all
} variables become quite long-named...

So don't name the array "fields", name it "f" or some such.  Three extra
characters (e.g. $f[capital] instead of $capital) for a whole lot less
complexity elsewhere.  And quote removal may be as easy as:

	f=( ${${(kv)f#\"}%\"} )

(Whether it's actually that easy depends on your input data, e.g., you
might also need to convert \" to ", but I think `read' will have done
that for you already.)


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

* Re: aliases not getting expanded inside functions?
  2003-01-13 18:48                     ` Bart Schaefer
@ 2003-01-13 21:40                       ` Carlos Carvalho
  2003-01-14  5:10                         ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Carlos Carvalho @ 2003-01-13 21:40 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer (schaefer@brasslantern.com) wrote on 13 January 2003 18:48:
 >On Jan 13,  3:59pm, Carlos Carvalho wrote:
 >}
 >}  >      fields=( "${(@s:;:)dataline}" )	# Answers your other question
 >} 
 >} Doesn't work for me:
 >
 >Hrm.  I was sure I'd tried it, but on your input I get the same result
 >you do.  Well, there's always:
 >
 >    IFS=';' eval 'fields=( $=dataline )'

Good, and it also works without the eval:

     IFS=';' fields=( $=dataline )

did it for me.

 >However, it occurs to me that with dataline='...;";";...' -- that is, a
 >quoted semicolon in the input data -- there's no quick solution that's
 >going to do exactly what you need.

Yes, I know. The only way to deal with this is to parse the line
character by character. I'm taking the risk.

 >So don't name the array "fields", name it "f" or some such.  Three extra
 >characters (e.g. $f[capital] instead of $capital) for a whole lot less
 >complexity elsewhere.  And quote removal may be as easy as:
 >
 >	f=( ${${(kv)f#\"}%\"} )
 >
 >(Whether it's actually that easy depends on your input data, e.g., you
 >might also need to convert \" to ", but I think `read' will have done
 >that for you already.)

Quite persuasive :-) (I hope this is still English)


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

* Re: aliases not getting expanded inside functions?
  2003-01-13 21:40                       ` Carlos Carvalho
@ 2003-01-14  5:10                         ` Bart Schaefer
  2003-01-14 11:44                           ` Roman Neuhauser
  0 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2003-01-14  5:10 UTC (permalink / raw)
  To: zsh-users

On Jan 13,  7:40pm, Carlos Carvalho wrote:
}
}  >Hrm.  I was sure I'd tried it, but on your input I get the same result
}  >you do.  Well, there's always:
}  >
}  >    IFS=';' eval 'fields=( $=dataline )'
} 
} Good, and it also works without the eval:
} 
}      IFS=';' fields=( $=dataline )
} 
} did it for me.

Er, yes, I'm sure it did, but I think you'll find that after that line
is finished, IFS remains set to ';'.  The `eval' is in there so that IFS
will be automatically reset to its old value after the command finishes.

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


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

* Re: aliases not getting expanded inside functions?
  2003-01-14  5:10                         ` Bart Schaefer
@ 2003-01-14 11:44                           ` Roman Neuhauser
  0 siblings, 0 replies; 16+ messages in thread
From: Roman Neuhauser @ 2003-01-14 11:44 UTC (permalink / raw)
  To: zsh-users

# schaefer@brasslantern.com / 2003-01-14 05:10:08 +0000:
> On Jan 13,  7:40pm, Carlos Carvalho wrote:
> }
> }  >Hrm.  I was sure I'd tried it, but on your input I get the same result
> }  >you do.  Well, there's always:
> }  >
> }  >    IFS=';' eval 'fields=( $=dataline )'
> } 
> } Good, and it also works without the eval:
> } 
> }      IFS=';' fields=( $=dataline )
> } 
> } did it for me.
> 
> Er, yes, I'm sure it did, but I think you'll find that after that line
> is finished, IFS remains set to ';'.  The `eval' is in there so that IFS
> will be automatically reset to its old value after the command finishes.

    Wouldn't this work?

        OIFS=$IFS
        IFS=';' fields=( $=dataline )
        IFS=$OIFS

    That makes the intention obvious IMO.

-- 
If you cc me or remove the list(s) completely I'll most likely ignore
your message.    see http://www.eyrie.org./~eagle/faqs/questions.html


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

end of thread, other threads:[~2003-01-14 11:45 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-03 15:31 aliases not getting expanded inside functions? 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
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

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