Hello zw, and welcome to the Monastery!

First, there is a right brace (curly bracket) missing, but that’s hard to see because of the way the code is formatted. A little reformatting —

use strict; use warnings; my $filename = 'gff.annotated.gtf'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Can't open $filen +ame: $!"; my @transcript_id = (); my @lines = <$fh>; foreach my $lines (@lines) { my @column= split /\t/, $lines; foreach $element (@column) { if ($element[16] eq '"u"' || '"x"' || '"i"' || '"s"') { push @transcript_id, $element[10]; } } print @transcript_id;

— and the missing brace is easily seen.

Second, you have use strict (good!), so $element needs to be declared with my:

foreach my $element (@column) # ^^

Third, $element is a scalar variable, which on each iteration of the loop holds a single element of the @column array. But the expressions $element[16] and $element[10] reference an entirely different variable, an array named @element which you haven’t declared. In the case of $element[16], you probably just want to use $element. In the case of $element[10], I’m not sure what you want to do; maybe you meant $column[10]?

Fourth, Perl syntax requires that you make each comparison separately:

if ($element eq 'u' || $element eq 'x' || $element eq 'i' || $element eq 's') { push @transcript_id, $column[10]; }

Fifth, note that in the above snippet I have removed the extra quotation marks. Your problem description implies that you want to test for equality with the character u, not with the 3-character string "u" as you have in your code.

Sixth (and finally!), I think the logic of your inner foreach loop is questionable. Do you want to push to @transcript_id on each match in the line, or only once per line if a match is found? If the latter, you need to break out of the loop after the first match:

foreach $element (@column) { if ($element eq 'u' || $element eq 'x' || $element eq 'i' || $element eq 's') { push @transcript_id, $column[10]; last; } }

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: if array contain push another array by Athanasius
in thread if array contain push another array by zw

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.