Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > traversing a hash two using serveral conditions

Reply
Thread Tools

traversing a hash two using serveral conditions

 
 
Me
Guest
Posts: n/a
 
      01-19-2006
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?
 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      01-19-2006
Me wrote:
> 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:umper. 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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-20-2006
Me <(E-Mail Removed)> wrote:


> Snippet of what I have so far:



If you say so.


> if ($day == $) {

^^
^^ what's that?


Post real code if you want real help.

Have you seen the Posting Guidelines that are posted here frequently?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-20-2006
Me <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
How can i download serveral files on one go? JimmySlam ASP General 1 04-10-2006 09:41 AM
Traversing Hash or XML? Which is Fast thomson ASP .Net 0 12-27-2005 05:57 AM
Rendering literal and serveral controls nested in web control at design time Stephen Miller ASP .Net Building Controls 1 01-02-2004 01:26 AM
What is the best way to pass a user identity between serveral ASP.NET web sites? Stan ASP .Net 6 08-22-2003 09:02 PM



Advertisments