Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Array rearrangement hangup.

by Andrew_Levenson (Hermit)
on Mar 15, 2006 at 23:08 UTC ( [id://536986]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I'm attempting to write an encryption (and matching decryption) program, but it just was not working. No warnings or anything, but it wasn't giving me the correct output. As for the encryption half, I found the section that wasn't working, but I can't figure out why. Anybody think they'll be able to help? I isolated the task and gave an example input.
my @array=(1,2,3,4,5,6,7,8); my @array2; my $length = @array; my $length2 = $length-1; my $i; for $i(0..$length2){ push @array2, $array[$i]; $i+=2; } for $i(1..$length){ push @array2, $array[$i]; $i+=2; } undef @array; @array=reverse(@array2); undef @array2; for $i(1..$length){ push @array2, $array[$i]; $i+=2; } for $i(0..$length2){ push @array2, $array[$i]; $i+=2; } undef @array; @array=reverse(@array2); print @array;
The desired output is "51627384" if that helps.

Replies are listed 'Best First'.
Re: Array rearrangement hangup.
by ikegami (Patriarch) on Mar 15, 2006 at 23:13 UTC
    The $i+=2 in your loop does nothing. The for will put the next value from the list into $i without checking its current value. You want
    for ($i=0; $i<=$length2; $i+=2){ push @array2, $array[$i]; } for ($i=1; $i<=$length; $i+=2){ push @array2, $array[$i]; }
    or
    for $i(0..$length/2){ push @array2, $array[$i*2]; } for $i(0..$length/2){ push @array2, $array[$i*2+1]; }
      ...Looking at it, that makes sense. It just reads a new value for i, doesn't it? Thanks.
        You also have an off-by-one error. $length contains the number of elements in the array, which is one beyond the end of the array. The $i+=2 in your loop does nothing. The for will put the next value from the list into $i without checking its current value. You want
        for ($i=0; $i<$length2; $i+=2){ push @array2, $array[$i]; } for ($i=1; $i<$length; $i+=2){ push @array2, $array[$i]; }
        or
        for $i(0..($length-1)/2){ push @array2, $array[$i*2]; } for $i(0..($length-1)/2){ push @array2, $array[$i*2+1]; }
        or
        for $i(0..$#array/2){ push @array2, $array[$i*2]; } for $i(0..$#array/2){ push @array2, $array[$i*2+1]; }

        That doesn't fix your decrpytion (which is totally wrong), but it puts you at a point where you can start having a look at it.

Re: Array rearrangement hangup.
by QM (Parson) on Mar 15, 2006 at 23:20 UTC
    for $i(0..$length2){ push @array2, $array[$i]; $i+=2; }
    Don't modify the loop variable in the loop. This is bad programming practice. If you want to do something like this, make the loop do it for you:
    for ($i=0;$i<=@array-2;$i+=2) { push @array2, $array[$i]; }
    For the next section:
    for $i(1..$length){ push @array2, $array[$i]; $i+=2; }
    Careful you don't step off the array. While this works, it's fragile and easy to break.

    You should use strict; and use warnings;, and then use the debugger to find out where it goes horribly wrong ;)

    I'd do it for you now, but I have to run for the carpool!

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Array rearrangement hangup.
by Anonymous Monk on Mar 15, 2006 at 23:34 UTC

    surprisingly, List::MoreUtils doesn't seem to have unzip

    sub unzip { my ($a, $b) = ([], []); while (@_) { push @$a, shift; push @$b, shift } [$a, $b] }
Re: Array rearrangement hangup.
by Anonymous Monk on Mar 16, 2006 at 07:09 UTC
    $ perl -le' my @array = 1 .. 8; print "@array"; @array = map { $array[ $_ + @array / 2 ], $array[ $_ ] } 0 .. $#array +/ 2; print "@array"; ' 1 2 3 4 5 6 7 8 5 1 6 2 7 3 8 4

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-29 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found