Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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 imbibing at the Monastery: (5)
As of 2024-04-24 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found