Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
The stupid question is the question not asked
 
PerlMonks  

Casting return value of unpack properly

by puterboy (Scribe)
on Feb 12, 2013 at 15:34 UTC ( #1018395=perlquestion: print w/ replies, xml ) Need Help??
puterboy has asked for the wisdom of the Perl Monks concerning the following question:

I am using unpack to unpack an element that consists of a 32 byte hex string plus an optional unsigned integer. If the integer exists, I want to join the 2 with an underscore.

I tried:
join("_", unpack("H32V*", $string));
but it always performs the join even if there is no second element. Adding an intermediate assignment, made it work:
my @temp = unpack("H32V*", $string); join("_", @temp);
But that seems kludgy and I would like to avoid it. So, is there any way to properly cast/assign the result of unpack so that an undefined second element is ignored by join without using an intermediate variable?

Comment on Casting return value of unpack properly
Select or Download Code
Re: Casting return value of unpack properly
by jethro (Monsignor) on Feb 12, 2013 at 16:00 UTC

    I got the following results with perl v5.14.2:

    my $string="1234567812345678"; print join("_", unpack("H32V*", $string)),"\n"; my @temp = unpack("H32V*", $string); print join("_", @temp),"\n"; $string="12345678123456781111"; print join("_", unpack("H32V*", $string)),"\n"; @temp = unpack("H32V*", $string); print join("_", @temp),"\n"; #prints: 31323348355037383132333435363738 31323348355037383132333435363738 31323334353637383132333435363738_825307441 31323334353637383132333435363738_825307441

    As you can see, no difference between the two forms

      You are correct... I'm not sure what changed but I could have sworn it wasn't working before....
      Edit: *whistle*
Re: Casting return value of unpack properly
by Anonymous Monk on Feb 12, 2013 at 16:00 UTC

    You could always throw in a quick grep {defined $_} to filter them out.

Re: Casting return value of unpack properly
by AnomalousMonk (Monsignor) on Feb 12, 2013 at 16:33 UTC
    ... it always performs the join even if there is no second element.

    By this, I think you mean that there is a  '_' present in all cases, even if it the last (and dangling) character in the unpack-ed string.

    I tried essentially the same code as jethro under ActiveState 5.8.9 and Strawberries 5.10.1.5, 5.12.3.0 and 5.14.2.1 and get no extraneous underscore.

    >perl -wMstrict -le "my $p = 'ABCDabcdABCDabcd' . pack 'V', 0x3039; print qq{'$p'}; ;; my $unp = join '_', unpack 'H32V*', $p; print qq{'$unp'}; ;; $p = 'ABCDabcdABCDabcd'; print qq{'$p'}; ;; $unp = join '_', unpack 'H32V*', $p; print qq{'$p'}; " 'ABCDabcdABCDabcd90 ' '41424344616263644142434461626364_12345' 'ABCDabcdABCDabcd' 'ABCDabcdABCDabcd'

    Can you supply a short, stand-alone code example that demonstrates the effect you seek to avoid?

      I can't replicate it now. Perhaps before I was using 'B*' or 'A*' instead of 'V*'... but not sure what happened. I apologize for taking your time on what seems to be a non-issue.

        Oh, well... Just one of those days... sigh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018395]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2014-04-23 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (554 votes), past polls