>>>>> "HP" == Harry Putnam <[email protected]> writes:
>> split discards the pattern split on. To keep it, place it in parentheses.
>>
>> my @elems = split( /(\d)/, $var );
HP> [...]
HP> Oh GACK... I knew that too.. but lost my wits I guess.
it is easy to remember with this line (from randal schwartz):
use regexes when you want to keep/grab something, use split when you
want to toss away something (the delimiters).
split is defined to delete the stuff that matches and return the rest in
a list. it is very core perl and you should know its basic functionality
in your core. the edge issues of split, special cases, can be looked up
as needed.
HP> if (@elems1){
HP> print "elems1 ( \`split(/(\\d\\d)/,$var)' ):
HP> has these elements:\n";
why are you using ` as a markers? it doesn't need escaping inside ""
strings. in fact one of the nice things about perl strings is the
choices of delimiters and you don't need to escape other delims inside
them. that \` is very noisy. also is that line wrap in your code as you
also have a \n in there. i know this is tossaway play code but you
should practice good coding styles here too.
HP> for(@elems1){
HP> print "I\'m an element -> <$_>\n";
HP> }
again, no need to escape ' in a "" string. why so verbose in your text?
i would just use Data::Dumper and be done with it.
print "ELEM1: ", Dumper \...@elems1 ;
why write all that extra code to see what is in an array?
HP> elems1 ( `split(/(\d\d)/,100421)' ):
HP> has these elements:
HP> I'm an element -> <>
that is because there is always a leading null string between the start
and a leading delimiter (which is 2 digits).
HP> I'm an element -> <10>
that was the grabbed delimiter!
HP> I'm an element -> <>
that is a returned null string BETWEEN 2 delimiters
HP> I'm an element -> <04>
HP> I'm an element -> <>
HP> I'm an element -> <21>
repeat. rinse. repeat.
HP> ------- ------- ---=--- ------- -------
HP> elems2 ( `split(/(..)/,100421)' ):
since all you have is digits in the data, this behaves just like the
previous test.
HP> elems3 ( `split(//,100421)' ):
that is splitting on the null string which exists BETWEEN each pair of
chars. it doesn't leave a null leading string for some good reason i
can't remember and i am not in the mood to rtfm. so its function will
always be to split a string into a list of its chars. classic idiom to
know. also m/(.)/sg will do the same thing.
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/