this is a little garbled, but i assume from the database fields you mention that the goal is to build a table of number of visits by each source to each destination?

one good approach to this kind of problem: first draw up the data structure which is most suited to holding the information you have in the relations you want, then write a script in two parts: first to build that structure, then to use it. in the end you'll often find some way of eliminating the middle step and - in your case - writing to the database directly as you while though the file.

in this case i'd suggest that the data structure you want is something like:

$totals = ( $source => { $destination => $count, }, );

ie, a hash of hashrefs each of which refers to a { destination => count } pair. which is easily built:

my %totals; while (<LOGFILE>) { $totals{$1}->{$2}++ if m/(...)(...)(...)/; }

and then you just need to loop on the constructed hash to feed it all into the database, with something like:

my $sth = $dbh->prepare('insert into totals (srcIP, dstIP, visit_count +) values (?, ?, ?)'); foreach my $source (keys %totals) { $sth->execute($source, $_, $totals{$source}->{$_}) for keys %{ $to +tals{$source} }; }

incidentally, and in case i've completely missed the point, the main trouble you're probably having is illustrated here:

%ips = ( $src => "$dst"); foreach $ws (keys(%ips)) { ... }

The %ips hash will have exactly one key, $src, so looping on it isn't doing much. Which suggests that you haven't quite grasped how perl's hashes work and how much they can do for you. Some time with perldata, perldsc and perllol will help, along with a bit of strictness and if possible the perl cookbook.


In reply to Re: Counting Occurences, (hash referencing another hash) by thpfft
in thread Counting Occurences, (hash referencing another hash) by penguinfuz

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.