perlthirst has asked for the wisdom of the Perl Monks concerning the following question:
hi,
I have two files.
file1
------
12131
34234
53435
46566
34522
file2
------
some content
some content
some content
some content
some content
blah. blah.
Code id -46566-
some content
some content
some content
some content
Event id -445778441211-
some content
some content
some content
some content
some content
some content
some content
Code id -12131-
some content
some content
some content
some content
some content
some content
some content
some content
some content
Event id -123443111131-
Code id -12342-
some content
some content
some content
some content
some content
some content
some content
some content
Event id -445987432141-
file1 contains the list of codes, and file2 contains, code with its id and after some lines corresponding event ids for that code id.
My output should have list of code ids and its corresponding event id.
I have written the following code to achieve the above requirement
use strict; use warnings; open (FH1, "file1"); open (FH2, "file2"); my ($code, $event, $line); my %data; while (<FH1>) { chomp; # assigning code. $code = $_; while ( <FH2> ) { chomp; if ( $_ =~ /\-$code\-/ ) { $line = $_; while ( $line !~ /Event id (.*)/ ) { $line = <FH2>; chomp; } if ( $line =~ /Event id (.*)/ ) { $data{$code} = $1; } } } seek(FH2,0,0); } print "OUTPUT"; print %data;
It works, but i want some other way which should be simple and effective.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parsing with two files
by targetsmart (Curate) on Feb 20, 2009 at 12:18 UTC | |
|
Re: parsing with two files
by Bloodnok (Vicar) on Feb 20, 2009 at 12:39 UTC | |
|
Re: parsing with two files
by puudeli (Pilgrim) on Feb 20, 2009 at 12:55 UTC | |
|
Re: parsing with two files
by repellent (Priest) on Feb 20, 2009 at 17:45 UTC |