Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Parsing for [] in a file

by HJ (Novice)
on Nov 17, 2011 at 17:35 UTC ( [id://938652]=perlquestion: print w/replies, xml ) Need Help??

HJ has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Parsing for [] in a file
by toolic (Bishop) on Nov 17, 2011 at 17:53 UTC
    This gives you your desired output:
    use warnings; use strict; my %results = (a => 1); foreach my $var (keys %results) { while (my $attr_line = <DATA>) { if ($attr_line =~ /^\Q$var/ ) { print $attr_line; } } } __DATA__ a|12 a[11]|1 a[11][11]|0 b|2 c|0
    Update: in response to to OP's update, read http://sscce.org

      Except it will only work for the first key in %results, as the position in DATA will be at eof.

      If you flip around the foreach and the while, it should do as intended.

      Update: If you load the variable names (%results), make sure that you use chomp.

      --MidLifeXis

        ah!that greatly worked. Thank you very much. swapping the foreach and while and chomping after foreach.
      Hi toolic, Yes it worked just for the first line. Thanks for you response!
Re: Parsing for [] in a file
by JavaFan (Canon) on Nov 17, 2011 at 20:21 UTC
    It seems that a shell one liner ought to do:
    $ grep -f -F $file1 $file2 > $file3
Re: Parsing for [] in a file
by Not_a_Number (Prior) on Nov 17, 2011 at 18:38 UTC
    I even tried this and I see no output for this though

    Are you sure that File2 still contains the data that you think it does?

    If you ran the first version of your code on it, the line:

    open FILE2, ">$f2" or die "Could not open file: $! \n";

    opens a file for writing, not reading. In other words, it clobbers any existing data in the file...

      Apologize. Dint meant to have a ">$f2". Had it corrected before posting it. Thanks for the reply.
Re: Parsing for [] in a file
by aaron_baugher (Curate) on Nov 17, 2011 at 19:00 UTC

    Open File1, read it line by line, parsing out the variables and saving them as the keys of a hash, with true values (like 1).

    Open File2, read it line by line, parsing out the variable at the beginning of the line, checking to see if that variable exists as a key in your hash, and printing the line if it does.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

Re: Parsing for [] in a file
by Marshall (Canon) on Nov 17, 2011 at 18:53 UTC
    I even tried this and I see no output for this though( logical more sensible than the prev scrpt?

    if($attr_line =~ /^\$var/ )
    should be
    if($attr_line =~ /^\Q$var/)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://938652]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found