Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Not meaning to add values to a hash

by flexvault (Monsignor)
on Dec 22, 2012 at 14:18 UTC ( #1010016=note: print w/replies, xml ) Need Help??

in reply to Not meaning to add values to a hash


Note: These are suggestions, or just some things to think about!

In your assignment code below (abbreviated), you need some testing for input accuracy.

$flag_assignments{$key}{"starrez"} = $info[0]; . . . $flag_assignments{$key}{"NR-Spring"} = $info[10];
  • How about a test for the exact number of commas? If the CSV record exists but only has 3 fields, the rest of your testing is going to be wrong.
  • What if one of the fields looks like:
    ,,"Jones, Mr.",,,
    How will that affect your hash?

Since the question about your script wouldn't be needed if the input data was 100%, try doing some reasonable verification before building the hash.

If you are building the input data, why not build it as a hash record, i.e.

my $record = qq|startez\t$info[0]$sep . . . |; ## $sep is a field +separator defined by you

You can 'split' on the separator("$sep") to get the record into fields, and a simple 'foreach' loop to build the hash by 'split'ing on tab("\t"). Then you know the hash is correct. You can write this record to a file or send it to a database. Perl doesn't care how big it is, and you won't either. If fields are added, your original data will still be correct.

If you don't have control over the input, then what will happen if one field is added in the middle of the record? Checking on the exact number of fields will at least let you know that something is wrong, and you can fix it.

Once you verify the input, your script will do what you want.

Good Luck...Ed

"Well done is better than well said." - Benjamin Franklin

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010016]
[marto]: meh, that's the last time I reply to finddata, I don't see an improvement in attitude or approach
Discipulus there are none so deaf as those who will not hear..
Discipulus who do not want to ear
[Corion]: marto: No, I haven't seen anything either and I'm not sure if learning-by- osmosis will work here
[marto]: I've experienced the behavior LanX described, outsourcing really opens your eyes to the situation :)
[Discipulus]: ;=)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-03-30 07:06 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.