Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translating Perl Syntax

Status
Not open for further replies.

jfreets2

Programmer
Joined
Apr 27, 2007
Messages
5
Location
US
Well my previous post got deleted so I will try again.

Can someone help me translate the following perl syntax. I think join combines a bunch of values with a string deliminator such as "key1 | key2" etc. But how does it work below?

myhashkey => join("|",
keys %MyHash,
map { my $c = $_;
$c =~ s/(\w)/$1./g;
( quotemeta $c, $_ ) }
sort { length $b <=> length $a }
values %MyHash)

Any help would be greatly appreciated.
 
You are correct, join() joins together a list of strings using whatever delimiter is the first argument to join(), in this case the pipe: | and assigns it to the scalar on the left.

The second argument is the hash keys of %MyHash, so that is the first field of the joined string, say a hash key is "foo", so now we have:

foo|

The third argument is the map{} function which will create it's own list from the list generated by the sort{} function which is sorting the values of %MyHash according to the byte length of the values. Say a value was: '$10.00 dollars @ 3 per/unit'.

The map function takes that and using the regexp on the left (map operates from left to right) substitues each word character [a-zA-Z0-9_] with itself but adds a dot, so now we have:

'$1.0.0.0. d.o.l.l.a.r.s. @ 3. p.e.r./u.n.i.t.'

then next part of the mapfunction uses the quotemeta() function to add a backslash in ront of all meta characters in the string, so now we have:

'\$1\.0\.0\.0\. d\.o\.l\.l\.a\.r\.s\. \@ 3\. p\.e\.r\.\/u\.n\.i\.t\.'

and the $_ after the quotemeta part just adds the original string to the sting that join is creating, so now we have:

foo|\$1\.0\.0\.0\. d\.o\.l\.l\.a\.r\.s\. \@ 3\. p\.e\.r\.\/u\.n\.i\.t\.|$10.00 dollars @ 3 per/unit











------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Awesome thank you very much your help. So essentially this logic produces the keys and the values in the same string with some modifications to the value in the middle?

'myKey | myModifiedValue | myValue'

Jason
 
Yes

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top