coldy has asked for the wisdom of the Perl Monks concerning the following question:
@a = (0,0,1,1,1,0)
needs to be
@b = (0,0,3,0)
I then need to go one step further, changing the new sequence into letters, where each letter corresponds to a different number (assuming my maximum number of 1's in a row is 15) My input is a file of 1's and 0's. Ive tried this code, but it wont work!
The @newseq array is always empty, I must be doing something wrong and initializing in the wrong place, but I cant see where. Any ideas on a better way to do this would also be appreciated. Thanks.my @result = split '', 'abcdefghijklmno'; my @arg1 = (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); my %rules; # Build the rules hash $rules{$arg1[$_]} = $result[$_] for 0 .. $#result; my @newseq = (); my $run = 0; my @bin = (); while ( <INFILE> ) { @bin= split(/ /, $_); for (my $i=0;$i<$#bin;$i++) { if ($bin[$i]==0){ $run=0; push @newseq,$run; } if ($bin[$i]==1 && $bin[$i+1]==0){ $run++; push @newseq, $run; } else { $run++; } } } for (1 .. $#newseq) { if ( $_ % 80 != 0){ print "$rules{$newseq[$_-1]}"} else{ print "$rules{$newseq[$_-1]}\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: length of 1's
by ikegami (Patriarch) on Apr 27, 2009 at 03:57 UTC | |
by coldy (Scribe) on Apr 27, 2009 at 04:51 UTC | |
by ikegami (Patriarch) on Apr 27, 2009 at 05:07 UTC | |
by coldy (Scribe) on Apr 27, 2009 at 05:27 UTC | |
by ikegami (Patriarch) on Apr 27, 2009 at 05:30 UTC | |
| |
by coldy (Scribe) on Apr 27, 2009 at 05:09 UTC | |
by ikegami (Patriarch) on Apr 27, 2009 at 05:13 UTC | |
|
Re: length of 1's
by vinoth.ree (Monsignor) on Apr 27, 2009 at 04:04 UTC | |
|
Re: length of 1's
by roubi (Hermit) on Apr 27, 2009 at 03:38 UTC | |
|
Re: length of 1's
by jwkrahn (Abbot) on Apr 27, 2009 at 10:42 UTC |