Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

<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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-16 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found