PDA

View Full Version : Can you guys explain to me a certain part of this code.

slavegod
Oct 4, 2009, 01:52 PM
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.

angelwatt
Oct 4, 2009, 03:15 PM
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.

lee1210
Oct 4, 2009, 03:34 PM
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

slavegod
Oct 4, 2009, 03:59 PM
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...

autorelease
Oct 4, 2009, 04:19 PM
\$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:
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

lee1210
Oct 4, 2009, 10:07 PM
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:
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

slavegod
Oct 5, 2009, 11:37 PM
Man finally figured it out... Thanks guys