Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: read CSV file line by line and create hash table

by choroba (Cardinal)
on Sep 19, 2014 at 15:04 UTC ( [id://1101222]=note: print w/replies, xml ) Need Help??


in reply to read CSV file line by line and create hash table

You're on the right track. The $row won't survive the loop, but the hash will, as it is declared outside the loop.
my $base = $row->{'Base Addr. (Hex)'}; $reghash{$base}{'End Addr.'} = $row->{'End Addr.'}; # etc.

Your sample data seem to hint, though, that the Base Address is not always unique (F00000 appears 3 times). There can't be a duplicate key in a hash.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: read CSV file line by line and create hash table
by lshokri02 (Novice) on Sep 19, 2014 at 15:11 UTC

    Ah I see, I do have a column that is unique numbers to each line, so I can use that as my key?

    my $base = $row->{'Base Addr. (Hex)'}; $reghash{$base}{'End Addr.'} = $row->{'End Addr.'};

    by this do you mean to say I should be repeating the process so it gets the values for all the 20 columns? Thank you for your input!

      Well, you could do a line for each column. But this is Perl, and There Is More Than One Way To Do It. The documentation for readline_hr() is silent on whether it returns a different hash for each read, but Text::CSV does in fact do this. If you're not nervous about relying on undocumented behavior, and if Text::CSV_XS behaves the same way (Text::CSV is the slower of the two, but does not require a compiler to install), you can simply assign $row to your hash element. But the devil is in the details. When I run what is more or less your code, I find that the data tend to have leading (and sometimes trailing) spaces. So the snippet that assigns the input to the hash becomes

      my $base = $row->{' base addr. (hex)'}; $reqhash{$base} = $row;

      Note that the hard-coded key you look things up in the hash with MUST match the key actually in the hash, both in case and in white space. A lookup on 'Base Addr. (hex)' will find nothing if your file actually contains ' base addr (hex)'. If at some point you are not getting the results you expect, print your data and see if it's what you expect. For example:

      use Data::Dumper; print Dumper( $row );

      For good form the 'use' should be at the top of your code with the other 'use' statements, but if you're just debugging I like to put it with the debugging code, so that when I delete it I delete everything. If you do this more than one place, you can 'use Data::Dumper' more than one place, since module loading is (usually) idempotent. There's probably a slight performance hit, but you are going to remove all the debugging code anyway.

      Also, this STILL does not address the problem of non-uniqueness of base addresses. You need to key by base address because that's what you want to sort by. If all you want is a sorted output you could do something like

      my $base = "$row->{' base addr. (hex)'} $row->{' end addr (hex)'}"; $reghash{$base} = $row;

      If you actually want to find and manipulate the individual data, you may have to go to nested hashes -- that is, something like

      my $base = $row->{' base addr. (hex)'}; my $end = $row->{' end addr (hex)'}"; $reghash{$base}{$end} = $row;

      Doing this requires you to know both base address and end address to access a specific row.

      You're doing a lot right in your code (three-argument open(), lexical file handles, checking status, 'use strict', 'use warnings'). Good going! These may already have saved you time and grief, and will save you more when you write more Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1101222]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found