Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^5: insert label into file

by kennethk (Abbot)
on Mar 20, 2009 at 18:23 UTC ( #752128=note: print w/replies, xml ) Need Help??


in reply to Re^4: insert label into file
in thread insert label into file

Your language is a bit unclear, so I apologize if the following is off point. I think the key collision is resulting from you wanting to store multiple records at once. Given that you have a fixed number of fields per record, comma delimited, you should be able to just process one record at a time. If you want to deal with them all at once, an array of hashes (perllol) really is the only logical data structure. The thing that strikes me as most odd with the provided log file is that it uses the same delimiter for fields and records. Assuming you have the entire logfile read into $string, the following should be a decent prototype for what you want to do.

while ($string) { my @array = split /\,/, $string, 34; my %hash = (Date => @array[0..32]); store_in_db(%hash); if (defined $array[33]) { $string = $array[33]; } else { undef $string } }

Replies are listed 'Best First'.
Re^6: insert label into file
by grashoper (Monk) on Mar 20, 2009 at 19:07 UTC
    actually one record at a time is fine, I don't really follow your example.. ok string contains the data, and @array is going to hold the data seperated into pairs, but why string,34; I don't understand that part, then next line is doing what, I just don't get it. I will look at fixing my data file over the weekend and read re-read perllol hopefully if i read enough it will start making some kind of sense. thanks for looking, perhaps after I have read some more this will make more sense to me.

      The magic numbers in my code come in because each line consists of 1 date followed 16 key-value pairs. This adds up to 33 pieces of data per record. If the string has more than these 33 pieces of information, then there must be at least one more record.

      Update: You can avoid magic numbers by doing something along the lines of:

      my @records = split /,\s*(?=\d{2}\/\d{2}\/\d{4}\s\d{2}\:\d{2}\:\d{2}\s +[AP]M)/, $string; foreach (@records) { my %hash = (Date => split /\,\s*/); insert_into_db(%hash); }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2022-05-19 03:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?