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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.