Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hai monks,
Is it possible to store the return values of unpack in two or more arrays. for example unpack("A12A12A24A10",$string);the first two strings(A12A12) should store in array @first ,the secon two strings(A24A10) should store in array @second ,
i tried
(@first,@second)=unpack("A12A12A24A10",$string); __DATA__ the second array seems empty.
this one works for me,
@first=unpack("A12A12A24A10",$_); @second=splice(@first,24); __DATA__ this one worked fine.

Is there any other efficient or precise solution to do that?.

Replies are listed 'Best First'.
Re: help needed in storing the return values of unpack
by BrowserUk (Patriarch) on Mar 23, 2006 at 10:42 UTC

    Use array slices:

    my( @a, @b, @c ); ( @a[0..19], @b[0..9], @c[0..3] ) = unpack 'C20C10C4', 'abcdefghijklmnopqrst1234567890fred'; print "@a\n@b\n@c"; 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 1 +15 116 49 50 51 52 53 54 55 56 57 48 102 114 101 100

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: help needed in storing the return values of unpack
by timos (Beadle) on Mar 23, 2006 at 09:14 UTC
    The first example can't work, because the first array eat's up all the data. You could do
    ($f[0],$f[1], $s[0], $s[1])=unpack("A12A12A24A10",$_);

    It is the same problem when you wan't to pass two arrays to a sub, and retrieve them as to seperate arrays. It is not possible, you have to pass them as array-references.
      hai monks,
      your answers will work nicely if i have only few templates in the unpack operation.
      but if we have 20 templates how can i manage to use your answer.
      unpack("U20A10A10A10x10A10A10A10x10A10x10A10A10x10A10A10",$string);
      How can i store the return values of this unpack operation in two arrays?.
      i mean "U20" in array @first and the remaining in array @second
      even "U20" itself returns 20 elements.How can i do that efficiently.

        Use splice. It's most likely most efficient. Least prone to errors due to writing out ling lists of elements, and scales to as many elements in each array as you need. Indeed the parameters for splice can be calcualted at run time so you can change the split point to suit whatever is required.


        DWIM is Perl's answer to Gödel

        Any array you give as a parameter will gobble up all remaining items. So the following will work for you I guess:

        my ($first, @rest) = unpack "U20A10A10A10", $string;
Re: help needed in storing the return values of unpack
by GrandFather (Saint) on Mar 23, 2006 at 09:11 UTC

    You could:

    ($first[0], $first[1], @second)=unpack("A12A12A24A10",$string);

    Gets a bit unwieldy with more than a few elements though and proably is not as efficient as splice in that case either.


    DWIM is Perl's answer to Gödel
Re: help needed in storing the return values of unpack
by Anonymous Monk on Mar 23, 2006 at 09:46 UTC
    hai Corion, Your answer will not work. since U20 will return 20 elements,so the first element will be stored in $first , and remaining 19 elements and the strings returned by A10A10A10 will be stored in the array @second.