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
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
| [reply] [d/l] [select] |
|
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 ?
| [reply] |
|
($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
| [reply] [d/l] |
Re: need to put values (from unpack) into array-ref
by BrowserUk (Patriarch) on Mar 28, 2006 at 10:59 UTC
|
#! 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.
| [reply] [d/l] |
|
#! 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.
| [reply] [d/l] |
|
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);
| [reply] [d/l] |
|
THATS IT!!!!!
Thanks a lot
Luca
| [reply] |
|
The parent requires Perl 5.8. The following is more portable:
my @a = (
[ 'start 0', $b =~ /(.)./g ],
[ 'start 1', $b =~ /.(.)/g ],
);
| [reply] [d/l] |
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" ;
}
| [reply] [d/l] |
Re: need to put values (from unpack) into array-ref
by jeanluca (Deacon) on Mar 28, 2006 at 10:51 UTC
|
#! /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 | [reply] [d/l] |
Re: need to put values (from unpack) into array-ref
by ikegami (Patriarch) on Mar 28, 2006 at 16:57 UTC
|
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.
| [reply] [d/l] [select] |
|
|