Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

slavegod

macrumors newbie
Original poster
Jun 8, 2009
12
0
use strict;
use warnings;

my @list = (4,5,6,7,1,2,3);
my @x;
my @y;
my @z;

my $bin = 0;

foreach my $e ( @list ) {
if ( $bin == 2){
unshift @z, $e;
$bin = 0;
} elsif ( $bin ) {
push @y, $e, $e+1;
$bin = 2;
} else {
push @x, $e;
$bin = 1;
}
}

print "x is: @x\n";
print "x is: @y\n";
print "z is: @z\n";


this is perl. I just don't get the part from my $bin ; to the end of the foreach loop... can someone please explain. Thanks.
 
What parts don't you get, the loop, the conditions, the shifting and unshifting? Try running the program, it may help you understand the code. It's pretty simple code.
 
I don't know what we could tell you that you wouldn't find out running it yourself.

For each three numbers in @list, the first is added to the end of @x, the second is added to the end of @y, then that value plus one is added to the end of @y, the third is added to the beginning of @z.

-Lee
 
i just don't get what the $bin is for?? and i ran the code but i just don't really get it.


x is: 4 7 3
y is: 5 6 1 2
z is: 2 6


can someone just explain to me why x is as is.. because this is a practice exam and my prof is kinda bad at explaining...
 
$bin indicates which array the number from @list gets put into. 0 indicates @x, 1 indicates @y, and 2 indicates @z.

If $bin is 0, $e gets added to the end of @x.
If $bin is 1, $e and $e+1 get added to the end of @y.
If $bin is 2, $e gets added to the beginning of @z.

The order of the conditions is kind of weird. It might make more sense rearranged like this:
Code:
foreach my $e ( @list ) {
  if ($bin == 0) {
    push @x, $e;
    $bin = 1;
  } elsif ($bin == 1) {
    push @y, $e, $e+1;
    $bin = 2;
  } elsif ($bin == 2) {
    unshift @z, $e;
    $bin = 0;
  }
}

# (Since $bin will always be 0, 1, or 2, this functions the same as the original code despite the rewritten conditions.)

I haven't written Perl in years, looking at this code brought back bad memories... :p
 
i just don't get what the $bin is for?? and i ran the code but i just don't really get it.


x is: 4 7 3
y is: 5 6 1 2
z is: 2 6


can someone just explain to me why x is as is.. because this is a practice exam and my prof is kinda bad at explaining...

$bin is for determining if you're at the 1st, 2nd, or 3rd position of the current set of three.. or put differently, whether the position modulo 3 of @list is 0,1, or 2.

Is is as it is because x has the 1st element of each set of three numbers in @list added to the end.

I think it would be best to step through the code bit by bit... this time around I did it for you, but you can do this yourself or you can add print statements to see what's going on each step of the way:
Code:
my $bin = 0;
foreach $e ( @list ) { //First run, $e is 4
…
  } else { // because bin is 0
    push @x, 4; // @x is now (4)
    $bin=1
  }
}

foreach $e ( @list ) { //Second run, $e is 5, $bin is 1
  } elsif ( $bin) { // $bin is not 2, but it is non-zero
    push @y, 5, 6; // @y is (5,6)
    $bin = 2;
  }
}

foreach $e ( @list ) { //Third run, $e is 6, $bin is 2
  if( $bin == 2) { //Since $bin is 2
    unshift @z, 6; //@z is now (6)
    $bin = 0;
  }
}

foreach $e ( @list ) { //Fourth run, $e is 7, $bin is 0
…
  } else { // because bin is 0
    push @x, 7; // @x is now (4,7)
    $bin=1
  }
}

foreach $e ( @list ) { //Fifth run, $e is 1, $bin is 1
  } elsif ( $bin) { // $bin is not 2, but it is non-zero
    push @y, 1, 2; // @y is (5,6,1,2)
    $bin = 2;
  }
}

foreach $e ( @list ) { //Sixth run, $e is 2, $bin is 2
  if( $bin == 2) { //Since $bin is 2
    unshift @z, 2; //@z is now (2,6)
    $bin = 0;
  }
}

foreach $e ( @list ) { //Seventh run, $e is 3, $bin is 0
…
  } else { // because bin is 0
    push @x, 3; // @x is now (4,7,3)
    $bin=1
  }
}

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.