in reply to sequence of positions to binary sequence

Quick and dirty solution ahead.
perl -e' @pos=qw(2 4 6 9); $max=0; for (0..(@pos -1)){ $bin[$pos[$_]]=1; $max=$pos[$_] if $max<$pos[$_]; } for (0..$max){ $bin[$_]=0 unless $bin[$_]; } print "@bin\n";'
Update: This assumes that the largest index in @pos is the length of your @bin array, which would work if you only needed to test for true/false on the array, indeed if you only need to test true/false then the second loop is unneeded as undef and 0 are both false ;)

Replies are listed 'Best First'.
Re^2: sequence of positions to binary sequence
by ELISHEVA (Prior) on Apr 16, 2009 at 09:16 UTC

    Thanks to autovivification, the direct calculation of max is unnecessary. To create an array whose final element is at the maximum position of a 1:

    my @bits; $bits[$_] = 1 for @pos; for (0..($#bits-1)) { $bits[$_]=0 unless defined($bits[$_]); }

    Best, beth

      Doh!, thanks