For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2006 > Use of the => operator









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Use of the => operator
niall.macpherson@ntlworld.com

2006-03-29, 4:00 am

I needed a bit of code to convert a string to the hex values for the
individual chars and found the following 2 examples


my $teststr = 'abcABC123XYZ';

my $hexstr = join (' ', map (sprintf("%2.2x", ord($_)), split(//,
$teststr))); ## method 1

$hexstr = unpack 'H* ' => $teststr; ## method 2

Both of these work fine , although the second one doesn't separate the
values with spaces (which I needed) so I did

$hexstr =~ s/(\d\d)/$1 /g;

Method 1 was a useful exercise in understanding how map works (which I
previously hadn't been too comfortable with), but I decided to go with
method 2 as it looks a bit cleaner.

I think I understand how unpack works , however I have only previously
seen the => operator used in initialising hashes and I am not clear how
it works in this context. I looked at perldoc perlop but couldn't spot
anything relevant.

I'm therefore unsure how I can insert the spaces in method 2 in the
same line because I don't understand how / why the => operator is used
here.

Can someone please explain ?

Thanks

niall.macpherson@ntlworld.com

2006-03-29, 7:59 am


niall.macpherson@ntlworld.com wrote:

> $hexstr =~ s/(\d\d)/$1 /g;
>


This should of course be

$hexstr =~ s/([0-9A-Fa-f][0-9A-Fa-f])/$1 /g;

Anno Siegel

2006-03-29, 7:59 am

<niall.macpherson@ntlworld.com> wrote in comp.lang.perl.misc:
> I needed a bit of code to convert a string to the hex values for the
> individual chars and found the following 2 examples
>
>
> my $teststr = 'abcABC123XYZ';
>
> my $hexstr = join (' ', map (sprintf("%2.2x", ord($_)), split(//,
> $teststr))); ## method 1
>
> $hexstr = unpack 'H* ' => $teststr; ## method 2


[...]

> I think I understand how unpack works , however I have only previously
> seen the => operator used in initialising hashes and I am not clear how
> it works in this context. I looked at perldoc perlop but couldn't spot
> anything relevant.


Look for "comma operator" in perlop.

As used above, "=>" is entirely equivalent to a comma.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Dr.Ruud

2006-03-29, 7:59 am

niall.macpherson@ntlworld.com schreef:

> $hexstr =~ s/([0-9A-Fa-f][0-9A-Fa-f])/$1 /g;


Alternatives for [0-9A-Fa-f]

(?i:[0-9a-f])

[[:xdigit:]]

--
Affijn, Ruud

"Gewoon is een tijger."
echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
Tad McClellan

2006-03-29, 7:00 pm

niall.macpherson@ntlworld.com <niall.macpherson@ntlworld.com> wrote:

> $hexstr = unpack 'H* ' => $teststr; ## method 2



> I have only previously
> seen the => operator used in initialising hashes and I am not clear how
> it works in this context. I looked at perldoc perlop but couldn't spot
> anything relevant.



See the "Comma Operator" section.


> I'm therefore unsure how I can insert the spaces in method 2 in the
> same line



Use unpack() in a list context instead of in a scalar context,
then join up the list elements the way you want them:

$hexstr = join ' ', unpack('H* ' => $teststr); # untested


> because I don't understand how / why the => operator is used
> here.
>
> Can someone please explain ?



It is just a comma, the same as if you had:

$hexstr = unpack 'H* ' , $teststr; ## method 2


So, it has no relationship to getting the spaces that you want.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Dr.Ruud

2006-03-29, 7:00 pm

Tad McClellan schreef:
> niall.macpherson@ntlworld.com wrote:


>
> It is just a comma, the same as if you had:
>
> $hexstr = unpack 'H* ' , $teststr;


See also:

perl -MO=Deparse -e '$hexstr = unpack q{H* } => $teststr'

--
Affijn, Ruud

"Gewoon is een tijger."
echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com