"I want to extract all the information to the left of the last comma."

There are a few ways...

$derer  = "Seattle, Washington, USA, NAME";
my @data = split(/,/, $derer);
pop(@data); 
# data will now include each seperate part, but
# you could optionally paste it back together...
$derer = join(',', @data);

....Or a regex...

$derer  = "Seattle, Washington, USA, NAME";
$derer =~ s/,.*?$//;

That removes the comma followed by 0 or more chars (as few as possible) to
the end of the string.  You could then split it if you wanted to seperate
the data.

"if($derer =~ /(.*?),([.*?,]*)(.*)/g) {"
"Why is $2 blank?"

$2 is blank because the question mark says "match as few as possible", and
in this case is has no reason to match any more than zero (0) chars.  In my
regex I use the $ to signify the end of the string to force is to match some
stuff, otherwise it would have match zero chars as well.

Rob


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Thursday, December 27, 2001 4:09 PM
To: [EMAIL PROTECTED]
Subject: Re: $1, $2, $3


HI,

I have a question.
I want to extract all the information to the left of the last comma.
If there are 3 commas I would like everything to the left of the third
comma.

$derer  = "Seattle, Washington, USA, NAME";   ## I only need Seattle,
Washington, USA

if($derer =~ /(.*?),([.*?,]*)(.*)/g) {             ## I figure Seattle is $1
Washington is $2 and USA is $3
                                   ## I put ([.*?,]*) to tag if there is
otherinformation and commas, as many times
                                   ## following the first one. This should
be
$2.
        print " \$1 = $1, \$2 = $2, \$3 = $3 \n";       ##  $1 is Seattle $2
is
blank  $3 is Washington, USA

        $Source = $1 . " ". $2 ;
}
print "source = $Source \n";                  ## Prints source = Seattle


What am I doing wrong?
Why is $2 blank?

-- 
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]

Reply via email to