Your problem description was not exact. You didn't tell and the example data didn't tell either whether multiple event id's can follow one code id. You once used plural for event ids and in another sentence you used the singular.

If only one event id can follow one code id, I thought I'd skip the multiple reading of file2. Instead using a lookup table I would parse the valid code id's in advance.

#! /usr/bin/perl use strict; use warnings; use Data::Dumper; # State machine use constant CODE => 'CODE'; use constant EVENT => 'EVENT'; # Codes pre-parsed from file1 my %codes = ( 12131 => 1, 34234 => 1, 53435 => 1, 46566 => 1, 34522 => 1, ); my %results; my ($code_id, $event_id); my $state = 'CODE'; my $re_code = qr{Code \s+ id \s+ -(\d+)- }x; my $re_event = qr{Event \s+ id \s+ (-\d+-) }x; LINE: while( <DATA> ) { chomp(); if( ($state eq CODE) && /$re_code/ ) { # Check that code is valid if( $codes{$1} ) { $code_id = $1; $state = EVENT; } else { next LINE; } } else { if( ($state eq EVENT) && /$re_event/ ) { $results{$code_id} = $1; $state = CODE; } else { next LINE; } } } print Dumper(\%results); 1; __DATA__ 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-

Output:

$VAR1 = { '12131' => '-123443111131-', '46566' => '-445778441211-' };

Update: removed the odd __DATA2__ tag from the end..

--
seek $her, $from, $everywhere if exists $true{love};

In reply to Re: parsing with two files by puudeli
in thread parsing with two files by perlthirst

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.