2
   

Anyone for some Perl?

 
 
dwk
 
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
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 2 • Views: 1,202 • Replies: 5
No top replies

 
Craven de Kere
 
  1  
Reply Wed 8 Oct, 2003 11:12 pm
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.
0 Replies
 
JoanneDorel
 
  1  
Reply Wed 8 Oct, 2003 11:25 pm
I though Pearl was Tejas beer to be consummed in the back seat of a '57 Chevy!
0 Replies
 
dwk
 
  1  
Reply Thu 9 Oct, 2003 05:37 am
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 Razz

Dave
0 Replies
 
Blackie Chan
 
  1  
Reply Thu 9 Oct, 2003 10:57 am
$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.
0 Replies
 
dwk
 
  1  
Reply Thu 9 Oct, 2003 08:57 pm
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
0 Replies
 
 

Related Topics

Webdevelopment and hosting - Question by harisit2005
Showing an Ico File - Discussion by Brandon9000
how to earn money in internet - Discussion by rizwanaraj
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
CSS Border style colors - Question by meesa
There is no Wisdom in Crowds - Discussion by ebrown p
THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
 
  1. Forums
  2. » Anyone for some Perl?
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 05/04/2024 at 02:30:10