If you are going to revisit a previous question then you should, at least, include a link to the earlier discussion so that we can see what advice has already been offered and ignored.

Update: I've now taken a closer look at your code and I can't see the problem that you are having. The code below is based on your code above and has been expanded to create a standalone program. It seems to work as expected and print out the whole line. I therefore suspect that your problem is in the bits of your code that you aren't showing us.

#!/usr/bin/perl use strict; use warnings; my @all = qw(7723 7725 9908 7765 7874); while (<DATA>) { for my $x (0..$#all) { if($_ =~ /$all[$x]/) { print $_; } } } __DATA__ 77876 8543 CA84985 54E 77873 8003 CA84985 54E 77875 7725 CA84985 54E 77872 8511 CA84985 54E 77873 8123 CA84985 54E 77822 9908 CA84985 54E 77819 8503 CA84985 54E 77826 8040 CA84985 54E 77822 7874 CA84985 54E 77884 8543 CA84985 54E 77809 7211 CA84985 54E

And the output I get is:

77875 7725 CA84985 54E 77822 9908 CA84985 54E 77822 7874 CA84985 54E

There is, of course, a potential bug in this code as it checks for the existance of your numbers anywhere in a line of input and you specifically want to check for it in the second column. You can therefore change the central loop of my code to look lie this (and note that I've also been able to change the match operator to an equality operator).

while (<DATA>) { my $code = (split)[1]; for my $x (0..$#all) { if($code eq $all[$x]) { print $_; } } }

Finally, there's no point in checking for more than one match, so it's worth exiting the inner loop once a match is found.

while (<DATA>) { my $code = (split)[1]; for my $x (0..$#all) { if($code eq $all[$x]) { print $_; last; } } }

In reply to Re: Help in using arrays by davorg
in thread Help in using arrays by rsriram

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.