Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap

BBS: Re: Perl string manipulation - September 19, 1998 at 14:52:30

You are here: irt.org | BBS | Re: Perl string manipulation [This BBS is closed]

Posted by Jason Nugent on September 19, 1998 at 14:52:30:

In Reply to: Re: Perl string manipulation posted by Jason Turner on September 18, 1998 at 17:16:59:

: : A space?? Man... sorry about that. Ok, I didn't see it :)

: HTML does horrible things to spaces. Believe me, that code I wrote was actually readable b4 it was posted, <g>

I kinda figured... :)

: : Good point about the password field. This all comes back to knowing your data. Instead of using a \w+ (which matches alphanumerics), you might consider using a [^\|]+ instead, which should match everything except |. Or just make your users use bad passwords :)

BTW, I know you meant [^|]+ really ;-)

Yeah, I did :)

: You gotta be careful of that though - see the method used to create $new[2], and then the explanation on how it broke on the null string. If that space is always there then it's not so much of a problem - like you say, you gotta know your data.

This is true. You might also want to consider using a lookahead (one of the great Perl 5 features) to check and see if something is there before attempting the match...
Avert your eyes.. this might hurt with bad spacing :)

if ($string =~ s/ (?=|[^|]+\+-+\+\|\[^|]+ |$)\|\w+\|\+-+\+\| ([^|]+) \|$/$1/)
$string = $1;
else {$string = "default value"; }

I just tried this and it works on the data that Bob listed before. It also doesn't modify the string if the password field is null, but instead puts "default value" in $string. The regex could probably be optimized some more (although I am using negated character classes instead of \w+ which IS faster), but I'm just trying to illustrate my point that regexs can still be used without breaking code.
I really like your uses of split () though. Very clean. Any opinion on how fast they are compared to a regex? My lookahead example won't even try to match if it can't match the lookahead so I might get by the split () functions on null strings, but what about matches? Hmmm.....

: I love these Perl threads :)

Me too.. let's see how long we can keep this one going :)



You are here: irt.org | BBS | Re: Perl string manipulation [This BBS is closed]

©2018 Martin Webb