traversing a hash two using serveral conditions

M

Me

I have an array of hash with the contains some of the following data:
Direction
Name
Usage
Day
Month
Year
Protocol


From this, I need to get an array of a"Usage" data for a given
"Protocol" for a x number of "Day"(s), two direcions.

Protocol1 = [45, 67 ,87, 76, 75] : Would be usage for
5 day for "Protocol1".


Snippet of what I have so far:
################
foreach my $p(@$protocols) {

foreach my $c ($day1..$day2) {

foreach my $ref (@usage) { #the array with the data
my $day = $ref->{Day};
if ($day == $) {
push(@in_proto,$ref->{Usage})
if ($ref->{direction} eq "In");
push(@out_proto,$ref->{Usage})
if ($ref->{direction} eq "Out");

}

}

}

}
################


Am I going in the right direction?
 
M

Mark Clements

Me said:
I have an array of hash with the contains some of the following data:
Direction
Name
Usage
Day
Month
Year
Protocol

Hi - that isn't a very good description of your data structure. We would
need to see an exact sample of your data, eg as per the output of
Data::Dumper. The smallest subset possible that fully demonstrates the
structure would be fine.
From this, I need to get an array of a"Usage" data for a given
"Protocol" for a x number of "Day"(s), two direcions.

Protocol1 = [45, 67 ,87, 76, 75] : Would be usage for
5 day for "Protocol1".


Snippet of what I have so far:
################
foreach my $p(@$protocols) {

foreach my $c ($day1..$day2) {

foreach my $ref (@usage) { #the array with the data
my $day = $ref->{Day};
if ($day == $) {
push(@in_proto,$ref->{Usage})
if ($ref->{direction} eq "In");
push(@out_proto,$ref->{Usage})
if ($ref->{direction} eq "Out");

}

}

}

}
################


Am I going in the right direction?

syntax error at - line 9, near ")
if"
syntax error at - line 17, near "}"
Execution of - aborted due to compilation errors.

You need to copy and paste working code, preferably with

use strict;
use warnings;

at the top.

Mark
 
A

Anno Siegel

Me said:
I have an array of hash with the contains some of the following data:
Direction
Name
Usage
Day
Month
Year
Protocol


From this, I need to get an array of a"Usage" data for a given
"Protocol" for a x number of "Day"(s), two direcions.

Protocol1 = [45, 67 ,87, 76, 75] : Would be usage for

This would all be much clearer if you had provided a few lines of
example data.
Snippet of what I have so far:

Please don't post a snippet, post code that we can run and test.
Your code references variables that are nowhere set. How, do you
suppose, should we tell if what you do is right when we don't know
what you are doing?
################
foreach my $p(@$protocols) {

What is in @$protocols? In fact, since you are never using $p in the
code, why is the loop there at all?
foreach my $c ($day1..$day2) {

You are making a lot of passes over @usage: The number of protocols
times the number of days. You should make one pass and extract the
data you need as they come by.
foreach my $ref (@usage) { #the array with the data
my $day = $ref->{Day};
if ($day == $) {
^
Is this supposed to be $c? As it stands, it is a syntax error.
push(@in_proto,$ref->{Usage})
if ($ref->{direction} eq "In");
push(@out_proto,$ref->{Usage})
if ($ref->{direction} eq "Out");

}

}

}

}
################


Am I going in the right direction?

I don't know, since neither your prose nor your code make entirely
clear what it is you want. It looks terribly inefficient, but that
may not be a problem.

If I understand your intention, this is one way to do it:

# Set up raw data
my @usage;
while ( <DATA> ) {
@{ $usage[ @usage]}{ qw(
Direction
Name
Usage
Day
Month
Year
Protocol)
} = split;
}

# This hash points to the two possible output arrays
my %inout = (
In => \ my @in_proto,
Out => \ my @out_proto,
);

# Define protocols to watch
my %proto = (
Protocol1 => 1,
Protocol2 => 1,
);

# Define days to watch
my ( $day1, $day2) = ( 1, 14);

# Extract usage data
push @{ $inout{ $_->{ Direction}} }, $_->{ Usage} for
grep $proto{ $_->{ Protocol}},
grep +( $day1 <= $_->{ Day} and $_->{ Day} <= $day2),
@usage;

print "In: (@in_proto)\n";
print "Out: (@out_proto)\n";

__DATA__
In Donald 45 12 6 2000 Protocol1
In Susy 67 12 6 2000 Protocol1
In Gerald 87 12 7 2000 Protocol1

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top