Hello Monks, Im trying to convert a sequence of 1's and 0's into s new sequence which is the length of 1's, so;

@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!

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"; } }
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.

In reply to length of 1's by coldy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.