zsh-users
 help / color / mirror / code / Atom feed
* parameter expansion question
@ 2003-01-18 23:32 Le Wang
  2003-01-19  6:33 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Le Wang @ 2003-01-18 23:32 UTC (permalink / raw)
  To: Zsh users list

Hi,

I can force a variable to be expanded by using
${~var}.  Is there any way I can force a string to be
expanded without assigning it to a variable first?

I'm after the same information for filename
generation.  For exampel:

Right now, if I want to see if the glob pattern
'(#i)${i%.rar}*.par' would match anything, I would do:

  parFile=`print (#i)${i%.rar}*.par`

if I use: 


  parFile=(#i)${i%.rar}*.par

Zsh thinks I want an array.

______________________________________________________________________ 
Post your free ad now! http://personals.yahoo.ca


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

* Re: parameter expansion question
  2003-01-18 23:32 parameter expansion question Le Wang
@ 2003-01-19  6:33 ` Bart Schaefer
  2003-01-19 22:33   ` Le Wang
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2003-01-19  6:33 UTC (permalink / raw)
  To: Zsh users list

On Jan 18,  6:32pm, Le Wang wrote:
}
} I can force a variable to be expanded by using
} ${~var}.

The only difference between ${var} and ${~var} is that in the latter
case the resulting value is subject to filename generation.  It does
not "force" anything -- if filename generation would not normally
occur in the context, using ${~var} will not cause it to happen.

Perhaps you understood that already, but it's a commom mistake.

} Is there any way I can force a string to be
} expanded without assigning it to a variable first?

What does "be expanded" mean to you?  I thought at first you were
talking about filename generation, but then you said:

} I'm after the same information for filename generation.

This question is almost meaningless.  In most contexts, strings are
subject to filename generation.  In contexts where filenames are not
generated, assigning to a variable won't help.

If "be expanded" means you want to use other parameter operations
such as ${var%pattern} on the string:  Yes, you can do that, like
this:

    zsh% print ${${:-some string here}% here}
    some string

} Right now, if I want to see if the glob pattern
} '(#i)${i%.rar}*.par' would match anything, I would do:
} 
}   parFile=`print (#i)${i%.rar}*.par`

And then what?  "Want to see if [it] would match" could mean several
things.  It could mean you want want to test whether there are any
matches, e.g. [[ -n $parFile ]], or that you just want to print out
what the matches are so you can look at them, or ...

I suspect you mean that you want to find out whether, for each file
matching "*.rar", there exists a file with the same root name and
the extension ".par" -- but I've no idea what you want to do with
that information once you have it.  Perhaps this will help:

    print (#i)*.rar(e:'REPLY=${REPLY%.rar}.par &&
                       [[ -f $REPLY ]] || reply=()':)

} if I use: 
} 
}   parFile=(#i)${i%.rar}*.par
} 
} Zsh thinks I want an array.

There are several things going on here.

Firstly, unless you use `setopt glob_assign', zsh doesn't do filename
generation during assignment to scalar variables, so you wouldn't get
what you expect there anyway.

Secondly, it is true that there's an ambiguity with using parens for
glob modifiers and using them to wrap array assignments.  This should
probably be considered a bug (agree, PWS?) which can be worked around
by using the "${:-string}" trick I showed above:

    parFile=${:-(#i)${i%.rar}*.par}

Finally, even if you use the workaround with glob_assign set, whether
the variable is set as a string or as an array depends on whether the
glob matches one or more than one file.  So you're probably better
off using an array to begin with, that is:

    parFiles=( (#i)${i%.rar}*.par )

which requires neither glob_assign (because filename generation IS
always done during array assignments) nor any strange workarounds.

-- 
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] 5+ messages in thread

* Re: parameter expansion question
  2003-01-19  6:33 ` Bart Schaefer
@ 2003-01-19 22:33   ` Le Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Le Wang @ 2003-01-19 22:33 UTC (permalink / raw)
  To: Zsh users list

 --- Bart Schaefer <schaefer@brasslantern.com> wrote:
> 

[...]

Thanks a lot Bart.  I filed away a lot of useful notes
from your answers.

--
Le

______________________________________________________________________ 
Post your free ad now! http://personals.yahoo.ca


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

* Re: parameter expansion question
  2004-01-18  0:28 Szekeres István
@ 2004-01-18  1:54 ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2004-01-18  1:54 UTC (permalink / raw)
  To: Zsh list

On Jan 18,  1:28am, Szekeres Istva'n wrote:
> 
> #this is NOT ok, it prints 1=a 2=b 3=
> foo='a::b'
> print_em ${(s/:/)foo}
> 
> It seems like in the second case the empty string is not passed as the
> 2nd parameter. How to specify the expansion so it will pass the empty
> string just like in the first example?

You have two problems.

The first is that the (s) flag always collapses consecutive occurrences
of the split character; unlike perl's split function, zsh's (s/:/) cannot
create an empty word in the middle of "::".

Even if that worked, though, empty arguments are deleted when not quoted,
so you would need to quote the parameter expansion.

As a workaround to the splitting problem, you can use "typeset -T" like
so:
	typeset -T foo splitfoo :

And then quote the expansion:
	print_em "$splitfoo[@]"

Note that it doesn't work properly to use "typeset -T" more than once on
the same scalar variable (that is, you can't tie the same scalar to more
than one array).


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

* parameter expansion question
@ 2004-01-18  0:28 Szekeres István
  2004-01-18  1:54 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Szekeres István @ 2004-01-18  0:28 UTC (permalink / raw)
  To: Zsh list

Hello,

I have a problem with parameter expansion, could you please take a look
at it?

I try to do the following:

print_em ()
{
	echo 1=$1 2=$2 3=$3
}

#this is ok, it prints 1=a 2= 3=b
print_em "a" "" "b"

#this is NOT ok, it prints 1=a 2=b 3=
foo='a::b'
print_em ${(s/:/)foo}

It seems like in the second case the empty string is not passed as the
2nd parameter. How to specify the expansion so it will pass the empty
string just like in the first example?

Thanks in advance,
Istvan







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

end of thread, other threads:[~2004-01-18  1:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-18 23:32 parameter expansion question Le Wang
2003-01-19  6:33 ` Bart Schaefer
2003-01-19 22:33   ` Le Wang
2004-01-18  0:28 Szekeres István
2004-01-18  1:54 ` Bart Schaefer

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