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

Re: How to store the indexes of a array

by flexvault (Monsignor)
on Oct 14, 2011 at 10:44 UTC ( [id://931472]=note: print w/replies, xml ) Need Help??


in reply to How to store the indexes of a array

I'm reading into your question, but I have a script that downloads a CSV file with about 50 fields per record and then parses it to add records to a Berkeley DB. Unfortunately, every once in a while, the producer of the CSV file changes or worse adds a field in the middle of the record. My solution was to add a file:

date 1 Account 19 Client 4 Address 1 5 Address 2 6 . . .

Note: The "Account \t 19" is just to show key and index don't have to be in sequential order.

The script reads this file into a hash where the first field is the column heading and second field is the index into the DB array (example code is untested):

my %hash = (); my @array = (); my $no = 0; while (<$Fields>) { my $var = $_; chomp($var); my ( $key, $index ) = split(/\t/,$var); $hash{ $key } = $index; $array[$no] = $key; $no++; }

The script then parses the first record(keys in hash) of the CSV file to verify all fields are in the same order as the $array and that nothing has changed. If something has changed, the script emails me but doesn't corrupt the DB. I then modify the above file to conform to the new data format. The file is the order of the input fields and the index is the location in the array that is written to the DB. If a new field has been added in the middle, I place it in key order in the file and the index is the next element at the end of the array.

Once the key order is verified the script then processes the actual data and updates the DB.

I hope this is similar to your problem.

Good Luck

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

Log In?
Username:
Password:

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

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

    No recent polls found