![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 3
Rep Power: 0
![]() |
can anyone explain the script to me?thanks!
#!/usr/bin/perl
print while $_=$_{$_}; $_='OTcommailDngATghiliaguans'; 1 while s/(.{5})(.{5})?/$_{$2}=$1,$2/e; print while $_=$_{$_}; |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
well I'll do my best...I'm no expert
print while $_=$_{$_}; # does nothing $_ is empty $_='OTcommailDngATghiliaguans'; # initialize $_ 1 while s/(.{5})(.{5})?/$_{$2}=$1,$2/e; ? match 0 or 1 (nongreedy) () () in regurlar expressions puts the matching expression in $1.. $2.. e means eval second part of regular expression. .{5} means match any 5 characters What this is doing is looping through the string and building a hash like $_{$2}=$1 $_{'mailD'} = OTcom; $_{'ngATg'} = mailD; $_{'hilia'} = ngATg; $_{'guans'} = hilia; $_{''} = guans; one important thing to keep in mind is that $_ the scalar, is a different variable from $_{} the hash you can run this to see whats going on in the while loop $_='OTcommailDngATghiliaguans';
print "\$_=($_) \$1=($1) \$2=($2)\n";
while ($_) {
s/(.{5})(.{5})?/$_{$2}=$1,$2/e;
print "\$_=($_) \$1=($1) \$2=($2)\n";
},$2 causes the value of $2 to get trucated from $_ after each loop..... not sure why print while $_=$_{$_}; #This will loop through the hash and print it. after the regular expression, $_ = '' but we built a hash where $_{''} = 'guans', so that gets printed now assign $_ = 'guans'; then $_{'guans'} = hilia, so print that now assign $_ = 'hilia' ....... until we try $_{'OTcom'} which doesn't exist, so the loop is ended. result : guanshiliangATgmailDOTcom is printed. so in other words it's a very cryptic way to print the string out backwards 5 character chunks at a time, but you probably knew that part Last edited by spydoor; Apr 14th, 2005 at 1:58 PM. |
|
|
|
|
|
#3 |
|
Professional Programmer
|
Please tell all this wasn't just to decipher someone's sig/email addy. lol
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2005
Posts: 3
Rep Power: 0
![]() |
Thank you very much.
"$2 causes the value of $2 to get trucated from $_ after each loop..... not sure why ?" s/(.{5})(.{5})?/$_{$2}=$1,$2/e; my opinion, the sentence above has two function: s/(.{5})(.{5})?/$2/; (substitute $1$2 with $2) $_{$2}=$1,(input $1 into hash) I think the role of "/e" is to disreagard all components before "," when substitution begins. so for s/(.{5})(.{5})(.{5})?/$_{$2}=$1,$2,$3/e; will substitute $1$2$3 with $3. "so in other words it's a very cryptic way to print the string out backwards 5 character chunks at a time, but you probably knew that part" I don't know it before. Your help is really valuable. Thanks. I don't know hash before. An interesting thing is that if you use "$_=worldpeacewalksaway?world" The script will go on and on. I don't know if it is because we have the same two keys "world" here for hash. Have you heard of it? thanks! |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
Yes, because 'world' is there twice, it loops infinitely.
You need to understand what a hash (associative array) is, to really see what is going on. For this example it's functioning like a lookup table. $_{KEY} = VALUE $_{'peace'}='world' $_{'walks'}='peace' $_{'away?'}='walks' $_{'world'}='away?' $_{''}='world' while $_ = $_{$_}; # loop while I get a VALUE; assign KEY=VALUE after each iteration KEY = '' VALUE = 'world' KEY = 'world' VALUE = 'away?' KEY = 'away?' VALUE = 'walks' KEY = 'walks' VALUE = 'peace' KEY = 'peace' VALUE = 'world' KEY = 'world' VALUE = 'away?' #infinite looping begins . . . |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2005
Posts: 3
Rep Power: 0
![]() |
Thank you, Spydoor.
I understand now. for $_ = $_{$_}; I didn't notice the "$_" is changing before, I simply thought it would print the hash. But actually, it didn't exactly print hash. It needs the value from hash, but has its own process. You're very clever. Thanks for your friendliness. hui |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|