in reply to Adding object identifiers corresponding to matched headers and sub-headers.
<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).
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"; } } }
Tom Melly, pm@tomandlu.co.ukmap{$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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Adding object identifiers corresponding to matched headers and sub-headers.
by jdporter (Paladin) on Jan 19, 2007 at 23:52 UTC | |
by Melly (Chaplain) on Jan 22, 2007 at 11:17 UTC |