Thanks to everyone for the input.
It needs to parse a string for the file name and extension i.e.
(D:\temp\pic1.jpg and retrieve pic1.jpg). If there is no string to parse
then the string needs to be empty or null.
I just placed it in a subroutine:
sub GetFileName
{
my ($nm) = @_;
if($nm ne "")
{
$nm =~ /^(.*)[\\|\/](.*)$/;
return $2;
}
return "";
}
Thanks again,
Norman Pearson
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 31, 2002 3:45 AM
To: Norman Pearson
Cc: [EMAIL PROTECTED]
Subject: Clearing Backreference Variables?
Norman Pearson writes:
> How do you clear a backreference variable?
>
> For instance:
>
> $cnt = 1;
> for (1..4)
> {
> my $str;
> if($cnt < 3) { $str = "str1-str2"; }
> else { $str = ""; }
> $str =~ /(.*)-(.*)/;
> print "$cnt |$str| $2 \n";
> $cnt++;
> }
>
> Would like to print:
>
> 1 |str1-str2| str2
> 2 |str1-str2| str2
> 3 ||
> 4 ||
>
> Instead prints:
>
> 1 |str1-str2| str2
> 2 |str1-str2| str2
> 3 || str2
> 4 || str2
Firstly, regexes are redundant in your example. What you are doing can
be done more simply:
print "$_ |str1-str2| str2\n" for (1..2);
print "||\n" for (3..4);
Of course if that was not what you wanted, a more realistic example of
what you are trying to do would have been useful.
>
> How do you clear the $2 backreference variable?
You don't, they are read only. They only contain useful information if
the match was successful. So if you want to use then you must check
whether the match was successful first. For example:
use strict;
for my $cnt (1..4) {
my $str = "";
$str = "str1-str2" if $cnt < 3;
print "$cnt |$str|";
print " $2" if $str =~ /(.*)-(.*)/;
print "\n";
}
HTH
--
Brian Raven
Life gets boring, someone invents another necessity, and once again we
turn the crank on the screwjack of progress hoping that nobody gets
screwed.
-- Larry Wall in <[EMAIL PROTECTED]>
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/listinfo/activeperl