Hi Monks, I am kinda of a perl fumbler. I am stuck with a problem for more than an hour now, wish somebody cud give me pointers.

File1 :
a

File2 :
a|12
a[11]|1
a[11][11]|0
b|2
c|0

I want to pick only the variables from File2 that are in File1.

Output should hence be File3:
a|12
a[11]|1
a[11][11]|0

<my Script :
Note: %results has 'a' as a key>
open FILE2, "$f2" or die "Could not open file: $! \n"; //Corrected >$f +2 foreach my $var (keys %results) { while( my $attr_line=<FILE2>) { if($attr_line =~ m/(.*?)\[/ ) { if (exists $results{$1}) { print $attr_line; } } } }

but my Output is
a|12

I even tried this and I see no output for this though( logical more sensible than the prev scrpt?

open FILE2, "$f2" or die "Could not open file: $! \n"; foreach my $var (keys %results) { while( my $attr_line=<FILE2>) { if($attr_line =~ /^\$var/ ) { print $attr_line; <strike>print $var."\n"; </strike> } } } close(FILE2);

I understand the output is so because 'if exists' statement is true only for a|12. How do I make sure other any 'a*' be printed out too?

Thanks in advance, HJ

In reply to Parsing for [] in a file by HJ

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.