Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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?

Replies are listed 'Best First'.
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 AnomalousMonk (Canon) 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, and 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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018395]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2017-06-23 02:37 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (533 votes). Check out past polls.