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

Add part of one hash to another

by batrams (Novice)
on Feb 26, 2013 at 15:11 UTC ( [id://1020705]=perlquestion: print w/replies, xml ) Need Help??

batrams has asked for the wisdom of the Perl Monks concerning the following question:

Hi - I have two similar but different csv data files:

File A Field 0: Part Number Field 1: Description Field 2: Price

File B Field 0: Part Number Field 1: Description Field 2: New Part Number Field 3: Price

I read File A's data in using this code. I'm sure it can be done in other or more efficient ways but this works well for me:

################################# # reading data in from File A while ( <> ) { $csv->parse($_); push(@AoA, [$csv->fields]); } # load up the hash for my $i ( 0 .. $#AoA ) { $HoA{$AoA[$i][5]} = $AoA[$i]; } # end looping over rows #################################
I then read File B's data in a similar way:
################################# # reading data in from File B while ( <TEXTFILE> ) { $csv->parse($_); push(@H_USES, [$csv->fields]); } close(TEXTFILE); print "Done Loading USES Data\n"; # load up the hash for my $i ( 0 .. $#H_USES ) { $H_USES{$H_USES[$i][5]} = $H_USES[$i]; } # end looping over rows #################################
Now I want to append data from File B onto the the hash I made from File A. If the file structures were identical I could simply do this:
################################## # Add on File B's data for my $i ( 0 .. $#H_USES ) { $HoA{$H_USES[$i][5]} = $H_USES[$i]; } # end looping over rows ##################################
But of course they are not identical. I want to simply ignore or skip over Field 2 from File B. I'm having trouble cooking up how to do this, so I thought I'd ask the gurus here. Any ideas would be appreciated.

Replies are listed 'Best First'.
Re: Add part of one hash to another
by kennethk (Abbot) on Feb 26, 2013 at 15:23 UTC
    You will get a lot of traction here from Slices - the ability to specify a subset of an array. The actual structure is a little obtuse to me, but you''l probably want something like:
    ################################## # Add on File B's data for my $i ( 0 .. $#H_USES ) { $HoA{$H_USES[$i][5]} = [@{$H_USES[$i]}[0, 1, 3 .. $#{$H_USES[$i]}]]; } # end looping over rows ##################################

    You'd have something cleaner if you don't mind being destructive, because you can use splice:

    ################################## # Add on File B's data for my $i ( 0 .. $#H_USES ) { splice(@{$H_USES[$i]},2,1); # Eliminate 1 field at location 2 $HoA{$H_USES[$i][5]} = $H_USES[$i]; } # end looping over rows ##################################

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Add part of one hash to another
by mbethke (Hermit) on Feb 26, 2013 at 15:25 UTC
    If you just want to omit that field, you could read File B like this:
    while ( <TEXTFILE> ) { $csv->parse($_); push @H_USES, [ ($csv->fields)[0, 2..10] ]; # or whatever the numb +er of fields in the file }
    Looks a bit simple ... have I understood the problem correctly? :)

      >Looks a bit simple ... have I understood the problem correctly? :)

      Hi - I need the complete data from File B for other things later in the program, but both of the responses provided me with valuable examples and I appreciate all the help!
        I need the complete data from File B for other things later in the program
        Here is another way which may help:
        # reading data in from File A while ( <> ) { $csv->parse($_); push(@AoA, [$csv->fields]); } # reading data in from File B while ( <TEXTFILE> ) { $csv->parse($_); my @fields = $csv->fields; push(@H_USES, [@fields]); # Make a copy of the fields my @array = @fields; # Make the new array same format as File A splice(@array,2,1); # Remove Field 2 for example # Push the new array onto $AoA from File A push(@AoA, [@array]); } close(TEXTFILE); print "Done Loading USES Data\n"; # load up the hash $HoA for my $i ( 0 .. $#AoA ) { $HoA{$AoA[$i][5]} = $AoA[$i]; } # end looping over rows # load up the hash $H_USES for my $i ( 0 .. $#H_USES ) { $H_USES{$H_USES[$i][5]} = $H_USES[$i]; } # end looping over rows
Re: Add part of one hash to another
by nvivek (Vicar) on Feb 27, 2013 at 05:49 UTC

    As per your code, it seems you try to parse the data read from csv file and then you store each fields and values into an array. From that array, you store those values into hash. Here, I have confusion, $HoA{$AoA$i5}, why you have used 5 as an index for all elements. Kindly clarify it further progress.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1020705]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found