Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^3: read CSV file line by line and create hash table by Anonymous Monk
in thread read CSV file line by line and create hash table by lshokri02

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 studying the Monastery: (2)
As of 2024-04-26 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found