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


In reply to Re: sql and hash confusion by lostjimmy
in thread sql and hash confusion by grashoper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.