In another thread icrf made the comment that $& is "evil" which, at the very least, is perhaps a little - well, extreme? 
Now being lazy, I use this kind of construct to work my way through a string all of the time; like this:
$str = 'Mike_Lacey_is_a_lazy_programmer';
I find the first delimiter, an underscore character in this case, by searching for a _
/_/;
and that gives me the following values in some Perl pre-defined variables:
$` is set to 'Mike', everything before what was found.
$' is set to 'Lacey_is_a_lazy_programmer', everything *after* what was found.
and $& is set to what was actually found by the regex, '_' in this case.
The other approach, which I find difficult to read to be honest, would be to do something like this.
($wrd1,$wrd2,$wrd3,$wrd4,$wrd5,$wrd6) =
/(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)/;
or
/(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)/;
$wrd1 = $1;
$wrd2 = $2;
$wrd3 = $3;
$wrd4 = $4;
$wrd5 = $5;
$wrd6 = $6;
I do this so infrequently I've probably got the syntax wrong, so ignore that if I have please.
So - why is the second approach so popular? Is it more efficient? Does it just fit better with the way most people program?
Mike
Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
It's like this; even samurai have teddy bears, and even teddy bears get drunk.
Now being lazy, I use this kind of construct to work my way through a string all of the time; like this:
$str = 'Mike_Lacey_is_a_lazy_programmer';
I find the first delimiter, an underscore character in this case, by searching for a _
/_/;
and that gives me the following values in some Perl pre-defined variables:
$` is set to 'Mike', everything before what was found.
$' is set to 'Lacey_is_a_lazy_programmer', everything *after* what was found.
and $& is set to what was actually found by the regex, '_' in this case.
The other approach, which I find difficult to read to be honest, would be to do something like this.
($wrd1,$wrd2,$wrd3,$wrd4,$wrd5,$wrd6) =
/(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)/;
or
/(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)/;
$wrd1 = $1;
$wrd2 = $2;
$wrd3 = $3;
$wrd4 = $4;
$wrd5 = $5;
$wrd6 = $6;
I do this so infrequently I've probably got the syntax wrong, so ignore that if I have please.
So - why is the second approach so popular? Is it more efficient? Does it just fit better with the way most people program?
Mike
Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
It's like this; even samurai have teddy bears, and even teddy bears get drunk.