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

Re^2: insert label into file

by grashoper (Monk)
on Mar 20, 2009 at 16:32 UTC ( #752105=note: print w/replies, xml ) Need Help??

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

I have repeat values, I read this but now I don't know how to proceed.. If your hash could have repeated values, the methods above will only find one of the associated keys. This may or may not worry you. If it does worry you, you can always reverse the hash into a hash of arrays instead:
while (($key, $value) = each %by_key) { push @{$key_list_by_value{$value}}, $key; }
I am confused enough just using hash, hash of arrays, can I put it back into a hash somehow?, reason I want the hash is to input the data into a table in sql, using sql abstract, which looks like it should work for my purpose to store this data in a db, maybe I could somehow fix my data file to be correct by changing the way information is logged so that its numbers then names by using regexes to move the labels I have around.

Replies are listed 'Best First'.
Re^3: insert label into file
by kennethk (Abbot) on Mar 20, 2009 at 16:49 UTC
    The repeat values in your hash mean that you cannot use them as primary keys for identifying a data set. Clever data structures in Perl will not help you if you are trying to use them as the keys in your database. There are a couple possible solutions here, depending on your needs. The most natural solution is to use whatever you index your database with to key your hash. Can you provide your db table structure, some sample data from your original hash and explain how they are related?
      well let me clarify a bit here. My hash is backwards, data file example is below. Database has columns for Date,Login,SearchLoad,etc, Date column is a date/time login real searchload real when I read file into hash I get timevalue->columnname, I want the reverse which would be columname->timevalue. In order to fix my data file, I want to add a label Date to the first value which is a timestamp for each record throughout the file, then I want to read the file into a hash so I can input it into the database which is currently empty and only contains the columns I want to use for the table. The sql for the table creation is in the bottom of this comment.
      05/12/2006 12:21:10 AM,Login,19.748396,SearchLoad,13.459355,SearchCoun +t,64:0.660950,SearchResults,12.988677,SearchSave,6.138828,SearchDelet +e,1.682419,SearchDetails,10.114545,TaxLoad,3.995745,TaxResults,567:5. +287602,TaxDetails,3.975717,ClientAdd,2.643801,CMALoad,2.603744,CMASav +e,14.350635,CMADelete,1.021467,ClientDelete,0.731053,Logout,2.733931, +05/12/2006 12:54:07 AM,Login,18.015907,SearchLoad,13.259065,SearchCou +nt,64:0.620893,SearchResults,12.187526,SearchSave,5.327662,SearchDele +te,1.111600,SearchDetails,9.914256,TaxLoad,3.755399,TaxResults,567:5. +047257,TaxDetails,4.055832,ClientAdd,2.583715,CMALoad,2.834077,CMASav +e,14.350636,CMADelete,0.690993,ClientDelete,0.610879,Logout,2.743947, +05/12/2006 01:21:15 AM,
      database stuff..
      USE [performancetesting] GO /****** Object: Table [dbo].[AllResults] Script Date: 03/20/2009 1 +3:35:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[AllResults]( [Date] [datetime] NOT NULL, [Login] [real] NULL, [SearchLoad] [real] NULL, [SearchCount] [real] NULL, [SearchResults] [real] NULL, [SearchSave] [real] NULL, [SearchDelete] [real] NULL, [SearchDetails] [real] NULL, [ClientAdd] [real] NULL, [CMALoad] [real] NULL, [CMASave] [real] NULL, [CMADelete] [real] NULL, [ClientDelete] [real] NULL, [Logout] [real] NULL, [SiteCode] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NUL +L ) ON [PRIMARY]

        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 } }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-01-25 15:00 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (66 votes). Check out past polls.