Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

need to put values (from unpack) into array-ref

by jeanluca (Deacon)
on Mar 28, 2006 at 10:19 UTC ( [id://539664]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I've written the following not working code:
#! /usr/bin/perl # $b = "abefcdghijklmn" ; $a[0]->[0] = "start 0" ; $a[1]->[0] = "start 1" ; for($i = 0; $i < 14; $i += 2) { (push(@{$a[0]}), push(@{$a[1]}) ) = unpack("x$i A1 A1", $b) ; } foreach ( @{$a[0]} ) { print "$_\n" ; }
Using push like this doesn't work, but I hope I'm close to the true solution... :)
Any suggestions ?

Thanks Luca

Replies are listed 'Best First'.
Re: need to put values (from unpack) into array-ref
by davorg (Chancellor) on Mar 28, 2006 at 10:26 UTC

    It would be a lot easier to help if you told us what you were trying to achieve instead of just giving us non-working code and expecting us to work it out for ourselves.

    Also, adding use strict and use warnings to your code will help Perl to help you find the problems.

    I'm guessing, but is this something like what you wanted?

    #! /usr/bin/perl use strict; use warnings; my @a; my $b = "abefcdghijklmn" ; $a[0]->[0] = "start 0" ; $a[1]->[0] = "start 1" ; for(my $i = 0; $i < 14; $i += 2) { my ($foo, $bar) = unpack("x$i A1 A1", $b) ; push @{$a[0]}, $foo; push @{$a[1]}, $bar; } foreach ( @{$a[0]} ) { print "$_\n" ; }

    If that is what you want, then it's probably more maintainable if it's written something like this:

    #! /usr/bin/perl use strict; use warnings; my @a; my $b = "abefcdghijklmn" ; $a[0]->[0] = "start 0" ; $a[1]->[0] = "start 1" ; my @chars = split //, $b; while (@chars) { push @{$a[0]}, shift @chars; push @{$a[1]}, shift @chars; } foreach ( @{$a[0]} ) { print "$_\n" ; }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      yes, thats what I need (sorry for my short explanation).
      Is there no way to insert these values directly into those array refs with push or something else ?
        Is there no way to insert these values directly into those array refs with push or something else?

        Well, you could directly assign each value to the next element off the end of each array with code like this:

        ($a[0][@{$a[0]}], $a[1][@{$a[1]}]) = unpack("x$i A1 A1", $b) ;

        But that's horrible!

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: need to put values (from unpack) into array-ref
by BrowserUk (Patriarch) on Mar 28, 2006 at 10:59 UTC

    Maybe this more succinctly captures the essence of what your code has to do?

    #! perl -slw use strict; use Data::Dumper; my $b = "abefcdghijklmn"; my @a; @{ $a[ 0 ] } = ( 'start 0', unpack '(A1x)*', $b ); @{ $a[ 1 ] } = ( 'start 1', unpack '(xA1)*', $b ); print Dumper \@a; __END__ C:\test>539664 $VAR1 = [ [ 'start 0', 'a', 'e', 'c', 'g', 'i', 'k', 'm' ], [ 'start 1', 'b', 'f', 'd', 'h', 'j', 'l', 'n' ] ];

    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.

      Better yet.

      #! perl -slw use strict; use Data::Dumper; my $b = "abefcdghijklmn"; my @a = ( [ 'start 0', unpack '(A1x)*', $b ], [ 'start 1', unpack '(xA1)*', $b ] ); print Dumper \@a; __END__ C:\test>539664 $VAR1 = [ [ 'start 0', 'a', 'e', 'c', 'g', 'i', 'k', 'm' ], [ 'start 1', 'b', 'f', 'd', 'h', 'j', 'l', 'n' ] ];

      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.
        Here's another way, although it uses an additional temporary variable...
        my $b = "abefcdghijklmn"; my @a = (['start 0'], ['start 1']); my $i = 0; push @{ $a[$i++ % 2] }, $_ for unpack('(A)*', $b);

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        THATS IT!!!!!

        Thanks a lot
        Luca
      The parent requires Perl 5.8. The following is more portable:
      my @a = ( [ 'start 0', $b =~ /(.)./g ], [ 'start 1', $b =~ /.(.)/g ], );
Re: need to put values (from unpack) into array-ref
by salva (Canon) on Mar 28, 2006 at 11:48 UTC
    $b = "abefcdghijklmn" ; $a[0]->[0] = "start 0" ; $a[1]->[0] = "start 1" ; my @b = unpack 'A' x 14, $b; while (@b) { push @{$a[0]}, shift @b; push @{$a[1]}, shift @b; } foreach ( @{$a[0]} ) { print "$_\n" ; }
Re: need to put values (from unpack) into array-ref
by jeanluca (Deacon) on Mar 28, 2006 at 10:51 UTC
    Or
    #! /usr/bin/perl # use strict ; use warnings ; my $b = "abcdefghijklm" ; my @a ; $a[0]->[0] = "start 0" ; $a[1]->[0] = "start 1" ; for(my $i = 0; $i < 14; $i += 2) { push (@{$a[0]}, $_) for unpack("x$i A1", $b) ; push (@{$a[1]}, $_) for unpack("x".($i+1)." A1", $b) ; } foreach ( @{$a[0]} ) { print "$_\n" ; } foreach ( @{$a[1]} ) { print "$_\n" ; }
    Somehow I have the feeling this can be done all at once!!
    And is there an other way to use the ($i+1) inside unpack ?

    Luca
Re: need to put values (from unpack) into array-ref
by ikegami (Patriarch) on Mar 28, 2006 at 16:57 UTC

    Yuck! Using unpack is inappropriate here! Use substr:

    push(@{$a[$_%2]}, substr($b, $_, 1)) for 0..13;
    Alternatively:
    push(@{$a[0]}, $b =~ /(.)./g); push(@{$a[1]}, $b =~ /.(.)/g);
    And now for something fun (but not recommended):
    my $t = 1; push(@{$a[$t ^= 1]}, $_) foreach $b =~ /(.)/g;

    By the way, you shouldn't name your variables $b (or $a). These are reserved for sort.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2024-04-23 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found