in reply to Question about text::csv_xs

getline parses a single line, but if you want to keep the original lines (so that you can determine the lines that appear in both files), then I don't think it is very useful to you. How about this (untested!)?:

use strict; use warnings; use Text::CSV_XS; my ( $h1, $h2 ) = map get_parsed_lines( $_ ), qw( file_1 file_2 ); my %keep = map +( $_ => $h1->{ $_ } ), grep exists $h2->{ $_ }, keys %$h1; sub get_parsed_lines { my $file = shift; my $csv = Text::CSV_XS->new(); my %hash; open my $in, '<', $file or die "Read failed: $!\n"; while ( <$in> ) { $csv->parse( $_ ) or warn "Bad data: $_\n", next; $hash{ $_ } = [ $csv->fields() ]; } close $in or die "Failed to close $file: $!\n"; return \%hash; }

the lowliest monk

Replies are listed 'Best First'.
Re^2: Question about text::csv_xs
by Thargor (Scribe) on May 04, 2005 at 14:13 UTC
    Thankyou for your solution and it works up until some of the information from the 2 tables is different for same entry. So, say I have 1 person and part of the CSV is the date they posted something. One date was 4/25/2005 and another is 5/1/2005 I still only want to have 1 copy of this person in the combined CSV file. Since the solution puts everything into the Keys of the hash this causes a problem. I was wondering if there was an easy way to just get 1 of the fields of the csv file as the key to the has and put all of the information into the value? I hope that makes sense.

      Sure, that would very easy. Suppose you wanted to use the third field as hashkey. Then get_parsed_lines would be changed to:

      sub get_parsed_lines { my $file = shift; my $csv = Text::CSV_XS->new(); my %hash; open my $in, '<', $file or die "Read failed: $!\n"; while ( <$in> ) { $csv->parse( $_ ) or warn "Bad data: $_\n", next; my @row = $csv->fields(); $hash{ $row[ 2 ] } = \@row; } close $in or die "Failed to close $file: $!\n"; return \%hash; }
      More generally, you may want to use several fields, e.g., fields 0, 2 and 3. In this case, just change the last line in the loop to
      $hash{ join( ',', @row[ 0, 2, 3] ) } = \@row;

      the lowliest monk

        Hey thanks again that was what I ended up doing before I got a reply only problem is either I am displaying the data incorrectly on screen which is possible or all the values in the hass are the same. I only have 5 row cvs files that have 3 rows in common so the result is a 5 element hash problem is when I display the first value assigned in yhr ref'd array to a key it is always the same for every key which it definitely shouldn't be. what I am doing to display is a
        foreach $hashkey (keys %hash){ print "$hashkey has $hash{$hashkey}->[0] in first array element.\n" +; }
        and what I get is the same result for every key so maybe I am doing that wrong. Anyways thanks for the help in advance.

        Update: Ok the data looks like "lastname","firstname","date". What happens is that the %hash ends up having the keys tied to "lastname" so what is happening is when I print out the hash to the screen I get like
        Smith has Johnson in first array element.
        Johnson has Johnson in first array element.
        Laskey has Johnson in first array element.
        and so on.