Hi Experts,

I am learner in perl, i have question here please help me out

i have some files which contains data like below:

input

file 1 STD Area Name Amount 2 1 AB 1.0 2 2 AB-1 2.0 4 1 AB-4 1.0 4 2 AB-6 2.0
file 2 STD Area Name Amount 21 01 AB 4.0 22 12 AB-1 6.0 41 44 AB-4 9.0 42 46 AB-6 4.0 42 46 AB-6 4.0
file 3 STD Area Name Amount 01 10 AB 4.0 20 02 AB-1 6.0 40 04 AB-4 9.0 02 26 AB-6 4.0
file 4 STD Area Name Amount 56 01 AB 4.0 56 12 AB-1 6.0 65 44 AB-4 9.0 65 46 AB-6 4.0
Output: file 1 file 2 file 3 file 4 AB 1.0 4.0 4.0 4.0 AB-1 2.0 6.0 6.0 6.0 AB-4 1.0 9.0 9.0 9.0 AB-6 2.0 8.0 4.0 4.0

Each file contains common name where i need to add the Amount. can you please let me know how can i do this.

i tired as mentioned below:

my %result; my %hash = ( '2.1','AB', '2.2','AB-1', '4.1','AB-4', '4.2','AB-6', '21.01','AB', '22.12','AB-1', '41.44','AB-4', '42.46','AB-6', '42.46','AB-6', '01.10','AB', '20.02','AB-1', '40.04','AB-4', '02.26','AB-6', '56.01','AB', '56.12','AB-1', '65.44','AB-4', '65.46','AB-6'); foreach my $file (@files) { open FILE, "<$dir/$file" or warn "Couldn't open file ($!) or file ($!) + not found\n"; while (chomp ( my @fields = split /\t/, <FILE>) ) { my $key = "$fields[0].$fields[1]"; if (exists $hash{$key}) { $key = $hash{$key}; if (exists($result{$key})) { $result{$key} += $fields[03]; } else { $result{$key} += $fields[03]; } } } } close FILE;

In reply to Perl - Hashes by Perlseeker_1

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.