in reply to help needed in storing the return values of unpack

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.

Replies are listed 'Best First'.
Re^2: help needed in storing the return values of unpack
by Anonymous Monk on Mar 23, 2006 at 09:34 UTC
    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;
        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.