Gossamer Forum
Home : General : Perl Programming :

Help needed on the most efficient perl code for extraction.

Quote Reply
Help needed on the most efficient perl code for extraction.
Perl has many ways to perform surgery Smile What would the most efficient code be to extract "author" from this string...
$a = "author.234098333abc"
I need code for...
$userID = ???? what??? (to make $userID = "author".

I know there are a lot of different ways to do this but I want ideas on the most efficient?

Thanks
TimRyan
Quote Reply
Re: Help needed on the most efficient perl code for extraction. In reply to
All regex's have overhead penalties.

What about split ?

If this is your data:

$a = "author.234098333abc"

($userid) = split (/\./, $a);

should take the first stuff up to the period, and put it into $userid. This routine should have been pretty well optimized to do what you want to do, without extra overhead, but I dunno.

Untested, and I'm bleary eyed.

Quote Reply
Re: Help needed on the most efficient perl code for extraction. In reply to
Thanks pugdog that worked as...
$a = "author.234098333abc" ;
($userid) = split (/\./, $a);

with results of "author". Smile

My perl book only describes this...
$a = "author.234098333abc" ;
$userid = split (/\./, $a);

with results of "2" Frown

and as my perl book describes it will return a number of the position. But the book said nothing about putting the brackets around it ($userid)... The book said that "in an array context it returns a list of things that where found" so I never thought it would work on a scalar (single string).

Guess I need a new book Smile
THanks.
TimRyan


[This message has been edited by timryan (edited August 27, 1999).]
Quote Reply
Re: Help needed on the most efficient perl code for extraction. In reply to
Hi,

This is an interesting thing about perl. The array vs scalar context.

If you put parenthesis around the variable, you change the "context" of the right side from scalar, to array.

For:
Code:
$a = "author.234098333abc" ;
($userid) = split (/\./, $a);
'split' returns an array of the 'splits', so to pull off the first term, you'd use it as above (I just assumed that's all you cared about. 'join' is the reverse of split.

This:
Code:
$a = "author.234098333abc" ;
$userid = split (/\./, $a);
is one of the quirks of perl. By not putting the () around the $a, you are forcing a scalar context of the right side, which wants to return an array. So, perl does the next best thing, it returns the number of elements _in_ the array, which would be two -- since there is one period, the split returns 'author' and '234098333abc' which is two elements (0 and 1) in the array.

Regexes work the same way....

($a) =~ regex

will put the first result of the regex into $a.

$a =~ regex

will give '0' if the regex fails, or '1' if it succeeds.

That's why perl books usually start with discussions of 'context' even before operations <G>.

FWIW: the best perl book I've found is Perl Power! A jump start guide to programming with perl 5 - by Michael Schilli - pub: addison-wesley. For just language references "Perl Core Language Little Black Book" by Steven Holzner pub:coriolis is pretty good. There is also a pretty good book on RegEx's "Mastering Regular Expressions" by Jeffrey EFFriedl pub:OReilly.

www.postcards.com/infodir/amazon/perl_books.html

I'm starting a list of the books in my collection with reviews, but it's still in production. The above link has those books in clickable format -- and as always as a free site, we appreciate any orders from our site Smile





[This message has been edited by pugdog (edited August 27, 1999).]
Quote Reply
Re: Help needed on the most efficient perl code for extraction. In reply to
pugdog, Thanks for that nice explain Smile
See, coming from an old basic background I was looking at the example as a string and . was in the second position. BUT, by your explaination I looked at it as an array and element #2 made more sense now.

Also, thanks for the book suggestions. Will take a look when I get some $$$ together. Smile

THanks
TimRYan


[This message has been edited by timryan (edited August 27, 1999).]