Here are 2 solutions one which loops and which doesn't. Neither of which are
fantastically short (104 and 99 chars respectively)
pwd|perl -pe'$ENV{'PWD'}="";%e=reverse%ENV;$j=join("|",map
quotemeta,grep/./,keys%e);s/^($j)/\$$e{$1}/s'
pwd|perl
-pe'$ENV{'PWD'}="";%e=reverse%ENV;for$k(keys%e){if($k=~/./&&s/^$k/\$$e{$k}/){print;exit}}'
The 2 unfortunate problems are avoiding empty strings and the fact that
while
$PWD
is technically a correct result for all inputs it's not very userful!
If you're sure you have no blank env variable then
perl
-e'$_=delete$ENV{'PWD'};%e=reverse%ENV;for$k(keys%e){if(s/^$k/\$$e{$k}/){print;exit}}'
is shorter and pure perl but doesn't produce a newline
Fergal
On Fri, Sep 07, 2001 at 11:43:14AM -0400, Bernie Cosell wrote:
> Well, many moons ago, I wanted to do a similar thing, only I needed
> something different: I use a lot of shell variables to 'shortcut' to here
> and there in the system [$temp, $perl, $scripts, $news, etc]. What I
> wanted was that *IF* my current PWD had a prefix that matched some
> variable in my environment, THEN: I wanted to have my prompt look like:
> $perl/newclient
> or
> $scripts/backup/
> or
> $scripts/backup/tapewriter
>
> or the like... This was in the before-there-was-Perl days, and I DID
> find a way to do it [I wrote a little C program to do the job]... Might
> be an interesting exercise to do in Perl, it isn't all that hard [but
> might be tricky to get down to golf-style <72char form, [what with
> looping through %ENV looking for matching prefixes, etc...]
>
> /Bernie\
> --
> Bernie Cosell Fantasy Farm Fibers
> mailto:[EMAIL PROTECTED] Pearisburg, VA
> --> Too many people, too few sheep <--
--