perlnoobz has asked for the wisdom of the Perl Monks concerning the following question:
i do not understand the foreach loop..can anyone explain to me what is it doing?
#!/usr/bin/perl -w print "Your binary input please =>"; $bin = <STDIN>; #read in user input chomp $bin; # use of array to store $bin and split it @digits = reverse(split('',$bin)); #reverse and split $bin, then assig +n to an array $result = 0; foreach $pos (0..$#digits) { $result += $digits[$pos] * 2 ** $pos; } print $result;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: binary converter
by Limbic~Region (Chancellor) on Apr 22, 2013 at 15:30 UTC | |
by Jenda (Abbot) on Apr 23, 2013 at 12:20 UTC | |
by Limbic~Region (Chancellor) on Apr 23, 2013 at 17:25 UTC | |
|
Re: binary converter
by toolic (Bishop) on Apr 22, 2013 at 15:36 UTC | |
|
Re: binary converter
by CountOrlok (Friar) on Apr 22, 2013 at 15:18 UTC |