Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2005, 12:41 AM   #1
lunarny
Newbie
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0 lunarny is on a distinguished road
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 $_=$_{$_};
lunarny is offline   Reply With Quote
Old Apr 14th, 2005, 1:40 PM   #2
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
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.
spydoor is offline   Reply With Quote
Old Apr 14th, 2005, 2:06 PM   #3
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
Please tell all this wasn't just to decipher someone's sig/email addy. lol
__________________
Amateurs built the ark
Professionals built the Titanic

peace_of_mind is offline   Reply With Quote
Old Apr 15th, 2005, 1:27 AM   #4
lunarny
Newbie
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0 lunarny is on a distinguished road
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!
lunarny is offline   Reply With Quote
Old Apr 15th, 2005, 8:04 AM   #5
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
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
.
.
.
spydoor is offline   Reply With Quote
Old Apr 15th, 2005, 10:29 AM   #6
lunarny
Newbie
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0 lunarny is on a distinguished road
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
lunarny is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC