Reply
Wed 8 Oct, 2003 09:38 pm
Code:
$first =~ s/^(...)(.*)$/$2/;
$last =~ s/^(...)\-(.*)$/$2/;
Can someone explane to me what the following two substitution lines do. An example of the values they work on is $first = 453 and $last = W453.
I'm not to clear on what (...) means. As well what does $2 do?
Any information is greatly apreciated!
Dave
I knew nothing about perl 3 days ago. I've been working with perl non-stop since but fo not yet know enough to answer this. I hope some perl coders see this.
I though Pearl was Tejas beer to be consummed in the back seat of a '57 Chevy!
Yah, you and me both Craven. I lucked out and found a job designing websites. All their sites are done with Perl so i've been trying to learn all that i've forgotten about it heh. It does a lot of good stuff, to bad its so cryptic.
Good luck on your Perl adventures. As for me i'm gonna keep plugging away through my books
Dave
$first =~ s/^(...)(.*)$/$2/;
well, ^ will match the beginning of string in $first, (...) will match the next 3 characters in the string (and store the value in $1). (.*) would match the next 0 or more characters(and store it in $2). And $ would match the end of the string.
Therefore, if $first was "David", it would end up being "id" because $1 = "Dav", and $2 = "id".
$last =~ s/^(...)\-(.*)$/$2/;
If $last was "dwk-newbie", then $1 = "dwk", \- would match "-" and (.*) would match "newbie" which would be stored in $2.
Therefore, $last would end up being "newbie".
----------
So if $first = 453, since . matches a character, ... would match 3 characters 453, and (...) would match 453 and store it in $1. (.*) would match nothing since there are no characters after 453, so $first would print an empty string.
When $last = W453, "W453" would be matched and stored in $1, W454 doesn't have a "-" so we can't match the "-" so this substition would not happen. Therefore, $last would remain W453.
What is happening in this code is we have a initial value of 123-W456. We then need to add one to this and make it 123-W457.
I think what he was doing with that was trying to split up the first and last parts into $first and $last. Then he added one to $last and then put them together again.
To do that it would be more like this right?
$first =~ s/^(...)/$1/;
$last =~ s/^...\-(.*)$/$1/;
Then $first = 123 and $last = W456 right??
Then its
$last++;
$newnum = $first . '-' . $last;
Then $newnum = 123-W456 ??
But with the W in there that might not work. See what you think and hopefully I made some sense there hehe
Thanks for the help!
David