* path and += troubles
@ 2005-11-26 4:32 Steven Klass
2005-11-26 9:32 ` Christian Taylor
2005-11-26 17:00 ` Bart Schaefer
0 siblings, 2 replies; 6+ messages in thread
From: Steven Klass @ 2005-11-26 4:32 UTC (permalink / raw)
To: zsh-users
Hi all,
OK after reading the FAQ completely I am at my wits end. I want to
create three basic functions dealing with arrays
- Append
- Prepend
- Delete
The basic usage would be something along the lines of let's say I
had this nice array called path which looked like this:
echo $path
/usr/sbin /bin /foo /usr/local/bin
Then I did ran a couple of functions..
appendPath path /bin
prependPath path /sbin
deletePath path /foo
and ended up with...
echo $path
/sbin /usr/sbin /usr/local/bin /bin
My basic functions would be:
appendPath() {
# if the path exists delete it..
deletePath $1 $2
# Append the path to the end..
$1+=($2)
}
deletePath () {
# I cannot take credit for this - Borrowed from the net - but it
works :)
# I couldn't figure out how to use ${var#del}
local element
local build
build=()
eval '
foreach element in "$'"$1"'[@]"
do
if [[ -d "$element" && "$element" != $2 ]]
then
build=("$build[@]" "$element")
fi
done
'"$1"'=( "$build[@]" )
'
}
But THIS DOESN"T WORK!!! IT Should!! According to the FAQ += should
add the path to the array. Can someone help me with 2 things.
1. Why does += not work when adding to the array - It should right?
2. How will I handle prepend - I need to do a shift of all array items
Comments welcome thanks!!
---
Steven Klass
sklass@pointcircle.com
(480) 988-5657
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: path and += troubles
2005-11-26 4:32 path and += troubles Steven Klass
@ 2005-11-26 9:32 ` Christian Taylor
2005-11-26 13:25 ` Vincent Lefevre
2005-11-26 17:00 ` Bart Schaefer
1 sibling, 1 reply; 6+ messages in thread
From: Christian Taylor @ 2005-11-26 9:32 UTC (permalink / raw)
To: zsh-users
Steven Klass wrote:
> Hi all,
>
> OK after reading the FAQ completely I am at my wits end. I want to
> create three basic functions dealing with arrays
> - Append
> - Prepend
> - Delete
I don't know if this is exactly what you're looking for, but I think you can
save yourself some work by just using direct operations.
Prepending/Appending:
path=( prepended $path appended )
I never bothered about a deletion function, because I simply use:
typeset -U path
which will keep only the first occurence of any element.
Hope this is of some help,
Christian
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: path and += troubles
2005-11-26 9:32 ` Christian Taylor
@ 2005-11-26 13:25 ` Vincent Lefevre
2005-11-26 14:54 ` Steven Klass
0 siblings, 1 reply; 6+ messages in thread
From: Vincent Lefevre @ 2005-11-26 13:25 UTC (permalink / raw)
To: zsh-users
On 2005-11-26 10:32:35 +0100, Christian Taylor wrote:
> I never bothered about a deletion function, because I simply use:
> typeset -U path
> which will keep only the first occurence of any element.
But if a same path is written in two different ways, it doesn't
recognise it:
ay:~> echo $library_path
/home/lefevre/./lib /home/lefevre/./lib /home/lefevre/lib /usr/local/gmp/lib
ay:~> typeset -U LIBRARY_PATH
ay:~> echo $library_path
/home/lefevre/./lib /home/lefevre/lib /usr/local/gmp/lib
--
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: path and += troubles
2005-11-26 13:25 ` Vincent Lefevre
@ 2005-11-26 14:54 ` Steven Klass
2005-11-27 1:30 ` Vincent Lefevre
0 siblings, 1 reply; 6+ messages in thread
From: Steven Klass @ 2005-11-26 14:54 UTC (permalink / raw)
To: zsh-users
Hi,
This exposes one of my personal problems with typeset. If I really
want a path appended typeset -U will drop the appended path, in favor
of the path which already exists. So IMHO the only way to handle
this properly is to
Normalize the new path ($2) - Remove any trailing slashes ensure no
double-slashes
Check to see if the new path exists in the old path array ($1) and
delete it
Append or Prepend the new path to the $1
Here's some of the cleanup stuff I read about in this list..
path=($^path(N))
setopt EXTENDED_GLOB
path=(${${path//\/##/\/}%/})
unsetopt EXTENDED_GLOB
typeset -U path
But can anyone please explain my number 1 item - that is really
annoying me..
appendPath() {
# if the path exists delete it..
deletePath $1 $2
# Append the path to the end..
$1+=($2)
}
1. Why does += not work when adding to the array - It should right?
---
Steven Klass
sklass@pointcircle.com
(480) 988-5657
On Nov 26, 2005, at 6:25 AM, Vincent Lefevre wrote:
> On 2005-11-26 10:32:35 +0100, Christian Taylor wrote:
>> I never bothered about a deletion function, because I simply use:
>> typeset -U path
>> which will keep only the first occurence of any element.
>
> But if a same path is written in two different ways, it doesn't
> recognise it:
>
> ay:~> echo $library_path
> /home/lefevre/./lib /home/lefevre/./lib /home/lefevre/lib /usr/
> local/gmp/lib
> ay:~> typeset -U LIBRARY_PATH
> ay:~> echo $library_path
> /home/lefevre/./lib /home/lefevre/lib /usr/local/gmp/lib
>
> --
> Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
> 100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/
> blog/>
> Work: CR INRIA - computer arithmetic / SPACES project at LORIA
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: path and += troubles
2005-11-26 14:54 ` Steven Klass
@ 2005-11-27 1:30 ` Vincent Lefevre
0 siblings, 0 replies; 6+ messages in thread
From: Vincent Lefevre @ 2005-11-27 1:30 UTC (permalink / raw)
To: zsh-users
On 2005-11-26 07:54:35 -0700, Steven Klass wrote:
> Here's some of the cleanup stuff I read about in this list..
> path=($^path(N))
> setopt EXTENDED_GLOB
> path=(${${path//\/##/\/}%/})
> unsetopt EXTENDED_GLOB
> typeset -U path
This is a bad idea if the user has EXTENDED_GLOB set. Isn't it
possible to turn on EXTENDED_GLOB locally in parameter expansion?
> But can anyone please explain my number 1 item - that is really
> annoying me..
>
> appendPath() {
> # if the path exists delete it..
> deletePath $1 $2
> # Append the path to the end..
> $1+=($2)
> }
>
> 1. Why does += not work when adding to the array - It should right?
$1+=($2) doesn't make sense IMHO. You probably need an eval.
--
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: path and += troubles
2005-11-26 4:32 path and += troubles Steven Klass
2005-11-26 9:32 ` Christian Taylor
@ 2005-11-26 17:00 ` Bart Schaefer
1 sibling, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2005-11-26 17:00 UTC (permalink / raw)
To: zsh-users
On Nov 25, 9:32pm, Steven Klass wrote:
}
} # Append the path to the end..
} $1+=($2)
As you've discovered, this won't work. $1 is not an identifier (it's an
identifier dereference) so $1+= is not an assignment. Of course, you
need to have a recent-enough version of zsh to support += assignments.
For some reason "setopt nullglob" (even cshnullglob) prevents the error
message that would otherwise be produced for this line, turning it into
a silent no-op. I think that's probably an obscure bug.
Anyway, what you need is something like
eval $1'+=( $2 )'
} # I couldn't figure out how to use ${var#del}
That's probably because it's ${var:#del}.
eval $1'=( ${(P)1:#$2} )'
} 2. How will I handle prepend - I need to do a shift
eval $1'=( $2 ${(P)1} )'
or (better only for really huge arrays, and watch out for the ksharrays
setopt which changes the subscript offset):
eval $1'[1]=( $2 ${${(P)1}[1]} )'
You're probably better off doing everything in one assignment rather than
by having appendPath call deletePath. E.g.
appendElem() { eval $1'+=( $2 )' }
prependElem() { eval $1'=( $2 ${(P)1} )' }
deleteElem() { eval $1'=( ${(P)1:#$2} )' }
appendUniq() { eval $1'=( ${(P)1:#$2} $2 )' }
prependUniq() { eval $1'=( $2 ${(P)1:#$2} )' }
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2005-11-27 1:31 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-26 4:32 path and += troubles Steven Klass
2005-11-26 9:32 ` Christian Taylor
2005-11-26 13:25 ` Vincent Lefevre
2005-11-26 14:54 ` Steven Klass
2005-11-27 1:30 ` Vincent Lefevre
2005-11-26 17:00 ` 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).