Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: sql and hash confusion

by lostjimmy (Chaplain)
on Mar 19, 2009 at 19:42 UTC ( [id://751848]=note: print w/replies, xml ) Need Help??


in reply to sql and hash confusion

If you used Data::Dumper to look at your hash, you might be able to see more easily why things were screwing up. The first key you end up with is 1/5/2009 3:55:30 PM, which has a value of "Login". That's not what you want. You want to strip off the date beforehand.

I imagine that the dates are an important piece of information, so I don't think you'll want to get rid of them outright. And anyway, even if you did get rid of the dates, you'll have problems putting this into a hash. Hashes can only have one value per key, and your values repeat for each login record, so you'll just overwrite the first set of values.

If this is always the format of the data, then you could do something like the following:

use strict; use warnings; my $file = 'data.txt'; open my $fh, '<', $file or die "failed to open $file: $!"; my @tokens = split /,/, <$fh>; close $fh; chomp @tokens; my $found_logout = 1; my $date; my $i = 0; while ($i < @tokens) { if ($found_logout) { $date = $tokens[$i++]; $found_logout = 0; print "*****************************\n"; print "New date: $date\n"; } else { my $key = $tokens[$i++]; my $value = $tokens[$i++]; print "$key = $value\n"; # do DB stuff with $key and $value $found_logout = 1 if $key eq "Logout"; } }

I haven't used Win32::ODBC, but hopefully someone else can help you out with that part.

Update: And if you really need it in a hash:

my $found_logout = 1; my $i = 0; my %hash; while ($i < @tokens) { if ($found_logout) { %hash = (date => $tokens[$i++]); $found_logout = 0; } else { my $key = $tokens[$i++]; my $value = $tokens[$i++]; $hash{$key} = $value; if ($key eq "Logout") { $found_logout = 1; do_db_stuff(\%hash); } } } sub do_db_stuff { print Dumper shift; }

Replies are listed 'Best First'.
Re^2: sql and hash confusion
by grashoper (Monk) on Mar 20, 2009 at 12:21 UTC
    you are correct what I want to do with the date is add a new key called date into the hash so that it maps date->value, then login->time etc, I was thinking that inputting the data would need the column name, followed by the value, fields are always in same order in the data file, I would also like to add a value for site which would contain a code indicating which account is being measured. I really don't know where to go for figuring out how to get this into sql which is my main problem, seems there are not many examples out there for using parameters in a query programmatically I am guessing because stored procedures are the preferred mechanism for doing so. I don't want to build a stored procedure for it as I am not that versed in sql. I guess that would be a viable option to pursue though.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found