Well how about:
my %paths;
my @final;
my $i = 0
$paths{$_} ||= $i++ for split /:/, $ENV{PATH};
$final[$paths{$_}] = $_ for keys %paths;
$ENV{PATH} = join ':', @final;
Tanton
-----Original Message-----
From: Jason Tiller
To: [EMAIL PROTECTED]
Sent: 9/17/2001 7:14 PM
Subject: Re: Removing duplicate PATH entries?
Hi, Jeff! :)
On Mon, 17 Sep 2001, Jeff 'japhy/Marillion' Pinyan wrote:
> On Sep 17, Jason Tiller said:
> It's pretty good. The only change I would make is to structure it in
a
> slightly more idiomatic fashion:
>
> my %paths;
> $paths{$_}++ for split /:/, $ENV{PATH};
> $ENV{PATH} = join ':', keys %paths;
Slick. ;> I was working along this direction until I realized I had
two hidden requirements, specifically:
1) The order of the PATH variable should not be changed.
2) Duplicate path entries must be removed from the *tail*, not the
head of the path.
So, your solution perfectly met the needs I laid out in my original
post. Thanks! However, req. #1 arose because I have some entries in
my PATH that have duplicate executables, such as
"c:/cygwin/bin/ftp.exe" and "c:/winnt/system32/ftp.exe". While
running in my Cygwin environment, I want to ensure that the Cygwin
paths are first in the path. I understand that your method makes no
attempt to maintain the original order of the path entries - which is
understandable, since I didn't indicate that mattered to me!
That's why I had already moved to the array solution and the
shift/push list manipulation.
> Well, regexes are A way, but they're not necessarily the best
> approach. You'll end up having to do a lot of textual comparisons
> (blegh) and the like.
> $ENV{PATH} =~ s{
> (?<! [^:] ) # make sure there's not a non-: behind us
> ([^:]+) : # match the path to \1, and match a :
> (?= # if it's followed by...
> (?: [^:]* : )* # any other paths
> \1 # and then itself
> (?! [^:] ) # and is NOT followed by a non-:
> )
> }{}gx; # remove it (and the colon that came after it)
This is very cool. :) And it makes sense (scary!). Thank you for
expanding my knowledge! I must say my mind seems to struggle with the
double-negatives to find the start and end of the path. I think I'd
prefer the alternation subexpressions you explicitly stated for me.
The notation isn't as compact as the double-negative, but I seem to
grok them better. I was trying to work on this myself, but the
boundaries were beyond my skill. I appreciate your help!
Unfortunately, it fails my #2 "hidden" requirement because it removes
entries from the head of the PATH, thus changing the path search order
and potentially finding the wrong executables.
Any way to make Perl do the regexp's sdrawkcab?
Thanks again,
---Jason Tiller
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]