<update>I've fixed a potential bug since the original posting - btw you don't specify what should happen if the header+subheader+object only appears in one file (either file 1 or file 2) - currently, they will still be reported.</update>

Kiran, try the following code. A couple of things (since I can't believe that this will be the end of it).

  1. You have some lines that look like sub-headers, but the first set of digits is an ip address, not a simple numeric-string - how should these be treated?
  2. You haven't really specified your output format.

BTW I doubt I'm doing you any favours writing your code - I don't know whether you are over your head at school or in a job, but you're digging yourself in deeper. Unless you're in management, or training for management, in which case you're doing fine. That said, learn to write better specifications (unless you're shooting for the golden ring, and going for upper management).

Here's the latest code...

use strict; # change these to change your input and output files my $file_1 = 'File1.dat'; my $file_2 = 'File2.dat'; my $out_file = 'Output.dat'; my %headers; my ($current_header1,$current_header2) = (0,0); open(INPUT, $file_1)||die "Cannot open $file_1 for read:$!\n"; foreach(<INPUT>){ # match headers - we'll be a little fussy to avoid false positives if(/^((\|(\d+\.){5}\d+){2}(\|.+){2})\|\d+\|\d+\s*$/){ $current_header1 = $1; $current_header2 = 0; } elsif(/^((\|\d+){2})\s*$/ and $current_header1){ $current_header2 = $1; } elsif(/^((\|\d+){2})\|(\d+)\s*$/ and $current_header1 and $current_h +eader2){ $headers{$current_header1}{$current_header2}{$1} = $2; } } close INPUT||die "WTF? Couldn't close $file_1:$!\n"; $current_header1 = 0; $current_header2 = 0; open(INPUT, $file_2)||die "Cannot open $file_2 for read:$!\n"; foreach(<INPUT>){ # match headers - we'll be a little fussy to avoid false positives if(/^((\|(\d+\.){5}\d+){2}(\|.+){2})\|\d+\|\d+\s*$/){ $current_header1 = $1; $current_header2 = 0; } elsif(/^((\|\d+){2})\s*$/ and $current_header1){ $current_header2 = $1; } elsif(/^((\|\d+){2})\|(\d+)\s*$/ and $current_header1 and $current_h +eader2){ $headers{$current_header1}{$current_header2}{$1} += $2; } } # output the results... open(OUT, ">$out_file")||die "Cannot open $out_file for write:$!\n"; foreach my $hkey(keys %headers){ print OUT "Header $hkey\n"; foreach my $subkey(keys %{$headers{$hkey}}){ print OUT "Subheader $subkey\n"; foreach my $objkey(keys %{$headers{$hkey}{$subkey}}){ print OUT "Object $objkey total:" . ${headers{$hkey}{$subkey}{$o +bjkey}} . "\n"; } } }
map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm@tomandlu.co.uk

In reply to Re: Adding object identifiers corresponding to matched headers and sub-headers. by Melly
in thread Adding object identifiers corresponding to matched headers and sub-headers. by Kiran Kumar K V N

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.