For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > Counting & arrays









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Counting & arrays
Michael Gargiullo

2006-05-22, 7:00 pm

It's been a while since I've used Perl and I need some help with a
multidimensional array.

I have a file that I need to compile some stats on.

I need to keep track of 'actions' and 'rules'. Yes, stats from a
firewall.

Both 'actions' and 'rules' need to be dynamic so if a rule is added,
it's automatically dealt with.

As I loop through the file I have both the current action and rule as a
variable.

I've tried to store them several ways...


my @actionrule;
$actionrule[$action][$rule]++;

I need some kind of loop to extract the data. I also need to know what
the action and rule of each count is.

I've tried a few while and foreach loops without success.

Within the loop I need to build a query (per record).

$query="insert into tmpfw set action=$action, rule=$rule, count=$count";

foreach $first (@actionrule){
foreach(@first){
print $_."\n";
}
}

Not only does it not print the counts, but how do I get the action and
rule associated with it?

Thanks,

Mike
Dave Gray

2006-05-22, 7:00 pm

On 5/22/06, Michael Gargiullo <mgargiullo@pvtpt.com> wrote:
> It's been a while since I've used Perl and I need some help with a
> multidimensional array.
>
> I have a file that I need to compile some stats on.
>
> I need to keep track of 'actions' and 'rules'. Yes, stats from a
> firewall.
>
> Both 'actions' and 'rules' need to be dynamic so if a rule is added,
> it's automatically dealt with.
>
> As I loop through the file I have both the current action and rule as a
> variable.
>
> I've tried to store them several ways...
>
>
> my @actionrule;
> $actionrule[$action][$rule]++;


In order for this to work as you expect, $action and $rule need to be
numeric. Is that the case? If not, do you need to preserve the order?

> I need some kind of loop to extract the data. I also need to know what
> the action and rule of each count is.
>
> I've tried a few while and foreach loops without success.
>
> Within the loop I need to build a query (per record).
>
> $query=3D"insert into tmpfw set action=3D$action, rule=3D$rule, count=3D$=

count";
>
> foreach $first (@actionrule){
> foreach(@first){
> print $_."\n";
> }
> }


You need to add 'use strict;' at the top of your program. Not only
will that help you see if you've made a typo, it will help us be able
to debug your program better. Also read this:
<http://perldoc.perl.org/perlreftut.html>

for my $rule (@actionrule) {
for my $i (@$rule) {
# do stuff
}
}

> Not only does it not print the counts, but how do I get the action and
> rule associated with it?
>
> Thanks,
>
> Mike
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

JupiterHost.Net

2006-05-22, 7:00 pm



Michael Gargiullo wrote:
> It's been a while since I've used Perl and I need some help with a
> multidimensional array.


AKA a HASH :) You want a hash not an array :)

> I have a file that I need to compile some stats on.
>
> I need to keep track of 'actions' and 'rules'. Yes, stats from a
> firewall.
>
> Both 'actions' and 'rules' need to be dynamic so if a rule is added,
> it's automatically dealt with.
>
> As I loop through the file I have both the current action and rule as a
> variable.
>
> I've tried to store them several ways...
>
>
> my @actionrule;
> $actionrule[$action][$rule]++;


$actionrule{$action}{$rule}++;

> I need some kind of loop to extract the data. I also need to know what
> the action and rule of each count is.


for my $action (keys %actionrule) {
print "Action is: $action\n";
for my $rule (keys %{ $actionrule{$action} }) {
print "\tRule is: $rule\n";
print "\t\tand its count is: $actionrule{$action}{$rule}\n";
}
}
Michael Gargiullo

2006-05-23, 3:58 am



-----Original Message-----
From: JupiterHost.Net [mailto:mlists@jupiterhost.net]
Sent: Monday, May 22, 2006 6:12 PM
To: beginners@perl.org
Subject: Re: Counting & arrays



Michael Gargiullo wrote:
> It's been a while since I've used Perl and I need some help with a
> multidimensional array.


AKA a HASH :) You want a hash not an array :)

> I have a file that I need to compile some stats on.
>
> I need to keep track of 'actions' and 'rules'. Yes, stats from a
> firewall.
>
> Both 'actions' and 'rules' need to be dynamic so if a rule is added,
> it's automatically dealt with.
>
> As I loop through the file I have both the current action and rule as

a
> variable.
>
> I've tried to store them several ways...
>
>
> my @actionrule;
> $actionrule[$action][$rule]++;


$actionrule{$action}{$rule}++;

> I need some kind of loop to extract the data. I also need to know what
> the action and rule of each count is.


for my $action (keys %actionrule) {
print "Action is: $action\n";
for my $rule (keys %{ $actionrule{$action} }) {
print "\tRule is: $rule\n";
print "\t\tand its count is: $actionrule{$action}{$rule}\n";
}
}



THANK YOU!!

This works beautifully! I was pulling my hair out.

Thanks again

-Mike
JupiterHost.Net

2006-05-23, 3:58 am

> for my $action (keys %actionrule) {
> print "Action is: $action\n";
> for my $rule (keys %{ $actionrule{$action} }) {
> print "\tRule is: $rule\n";
> print "\t\tand its count is: $actionrule{$action}{$rule}\n";
> }
> }
>
>
>
> THANK YOU!!
>
> This works beautifully! I was pulling my hair out.


Great, Perl programmers are only bald from genetics ;)

> Thanks again


np Mike :)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com