in reply to Re^3: Update XML Values using two primary keys
in thread Update XML Values using two primary keys

I have created a routine to read the CSV file as a hash but I don't get how to compare to the existing id. Here is my piece of code. Can you please help how to do the comparison? Thank you.

#!/usr/bin/perl use strict; use warnings; use Text::CSV; use Data::Dumper qw(Dumper); my $file = 'test_data.csv'; csv_hash($file); sub csv_hash { my ($filename) = @_; my $csv = Text::CSV->new ({binary => 1,auto_diag => 1,sep_char => + ';'}); open(my $data, '<:encoding(utf8)', $filename) or die "Could not op +en '$filename' $!\n"; my $header = $csv->getline($data); $csv->column_names($header); while (my $row = $csv->getline_hr($data)) { print(Dumper $row); } close $data; }

Replies are listed 'Best First'.
Re^5: Update XML Values using two primary keys
by hippo (Archbishop) on Jan 09, 2021 at 12:01 UTC

    Your code as presented is not building a HoH or indeed any single data structure at all from the CSV data. All it is doing is reading and printing it line by line. Replace your while loop with something like this (untested):

    my %HoH; while (my $row = $csv->getline_hr($data)) { my $key = "$row->{latitude}.$row->{longitude}"; # Compare next if exists $HoH{$key} && $row->{id} <= $HoH{$key}->{id}; # Set (or overwrite) $HoH{$key} = { name => $row->{name}, id => $row->{id} }; } # At this point I would return the hash (or a reference to it) but you + would need to change the calling line, of course

    See perldsc for more on such deep/nested structures.


    🦛