You are here: irt.org | BBS | Re: Perl string manipulation [This BBS is closed]
Posted by Jason Turner on September 18, 1998 at 03:32:52:
In Reply to: Re: Perl string manipulation posted by Jason Nugent on September 15, 1998 at 12:12:35:
: : I have a perl variable that contains this text
: : |password | +----------+ |rangers |
: : (Password is the column name, rangers the data).
: : Can anyone suggest how to extract the 'rangers' part of the string?
: $data =~ s/| (\w+)|$/$1/; # neato regex
s/neato/broken/ # :- (
: let's go through it:
I'll spare you that pain ;-)
TMTOWTDI, so here's 10.
#! /usr/bin/perl -w
for $i ('foo ','bar','H!LL ','pa$$ ','di da ',' ','') {
$data = "|password | +------+ |$i|";
print "\nData is now :",$data,":\n";
@new = ( 0 .. 9 ); # initialise array with some numbers
$new[0] = pop @{ [ split '\|' => $data ] };
($new[1]) = reverse split '\|' => $data;
$new[2] = $1 if ($data =~ /\| ([^|]+)\|$/);
$new[3] = (split '\|', $data)[3];
$data =~ /\| ([^|]*)\|$/ ; $new[4] = $1;
($new[5] = $data) =~ s/.*\| ([^|]*)\|$/$1/;
$new[6] = (split '\|' => $data, -1)[-2];
$new[7] = (reverse split '\|' => $data, -1)[1];
$new[8] = (split '\|', $data, -1)[3];
chop ($new[9] = pop @{[split '\|' => $data, 4]});
{ local $, = ":" ; print "Results ",@new,"\n" }
}
__END__
Some of the above are just different ways of writing the same thing.
However, some look the same, but they aren't... :-)
All 10 methods work OK when the field is not a null string
So if there is always that space appended, you could use any, and chop ().
On a null string
Methods 0 & 1 break, will return ' +-----+ ' - could be handy ?
Method 2 will hold it's initial value - prints '2'
so if it wasn't initialised, you could test defined () b4 use
Method 3 is undef. (see 8 for the difference)
Methods 4 to 9 are fine.
Bob, throw some of your data at it, tell us how they fair.
Jason.
Follow-ups:
You are here: irt.org | BBS | Re: Perl string manipulation [This BBS is closed]