At 15:16 -0400 7/9/2001, Morbus Iff wrote:
> > $f =~ m/:(.*)$/ ? print "$1\n" : print "$f\n";
>
>This will fail if you're more than one directory deep, ie, it'll work fine
>on "HD:filename.xml", but will fail on "HD:Directory:filename.xml". In the
>first case, it will properly return "filename.xml", in the second case, it
>will return "Directory:filename.xml".
>
>This small modification will fix that:
>
> $f =~ m/:([^:]$.*)$/ ? print "$1\n" : print "$f\n";
>
>
Actually, I don't understand the $ after the character class in that one.
Neither does the -w flag, although the right answer is generated without
the -w--or with it if your eye ignores the warning
Use of uninitialized value in concatenation (.) at t.pl line 9.
The path really only has one right end.
TIMTOWTDI
One could if desired eschew both the regular expression and the ?: operator:
$a="Disk:Folder:Folder2:file";
$b="file";
print substr($a, rindex($a, ':') + 1);
print "\n";
print substr($b, rindex($b, ':') + 1);
print "\n";
% perl -w t.pl
file
file
(conveniently for our purposes, rindex returns -1 for no match). [I
confess to running
This is perl, v5.6.0 built for darwin. rather than MacPerl.]
Which is "better"? The regular expression and ?: somehow may feel more
Perlish.
I think the rindex/substr for this particular task is probably easier to
read later (whether to use functional notation or operator notation is an
sub-choice).
Which is faster? Only measurement will tell. It probably doesn't matter
much unless one is dealing with thousands of files, but that too would have
to be measured.
--John
--
John Baxter [EMAIL PROTECTED] Port Ludlow, WA, USA