With a change to kcott's solution, this could be achieved. Your code shows you don't understand the terniary operator something ? 'if true' : 'if false'. The 2 lines with your error are:

if($start=$i)

and

if($end=$i)

Note how I wrote the line for my $start and my $end.

my $start = $i == 0 ? $data[$i - 1][2] + 1 : 'none'; my $end = $i == $#data ? 'none' : $data[$i+1][1]-1;

You could rewrite your code to be:

if ($special{$data[$i][0]}) { if($i == 0) { $start = 'none'; } else { $start = $data[$i - 1][2] - 1; } if($i == $#data) { $end = 'none'; } else { $end = $data[$i + 1][1] - 1; } print join "\t" => $id, $data[$i][0], $start, $end; }
The solution would be:
#!/usr/bin/env perl -l use strict; use warnings; my %special = (PF03797 => 1); { local $/ = "//\n"; while (<DATA>) { my ($id) = /^ID:(\w+)/; my @data; while (/HIT:(\w+).*?SEQ_START:(\d+).*?(\d+)/g) { push @data, [ $1, $2, $3 ]; } @data = sort { $a->[2] <=> $b->[2] } @data; for my $i (0 .. $#data) { if ($special{$data[$i][0]}) { my $start = $i == 0 ? 'none' : $data[$i - 1][2] + 1; my $end = $i == $#data ? 'none' : $data[$i+1][1]-1; print join "\t" => $id, $data[$i][0], $start, $end; } } } } __DATA__ ID:A0AWZ5 HIT:PF12951 SEQ_START:120 SEQ_END:350 HIT:PF03797 SEQ_START:822 SEQ_END:1073 HIT:PF15789 SEQ_START:1515 SEQ_END:1547 HIT:PF00267 SEQ_START:1200 SEQ_END:1350 //
The output from my code was:

A0AWZ5  PF03797 351     1199

Hope this helps.


In reply to Re^4: Stuck in my final step of code using array of arrays by Cristoforo
in thread Stuck in my final step of code using array of arrays by Anonymous Monk

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.