Ask a Question related to PERL Beginners, Design and Development.
-
George #1
Help with array, hashes and objects
I want to parse a long ns log text file, and I've attempted to write some
perl code to do that. I am a newbie, and I haven't managed to get my code
working.
Basically, the ns file contains information about TCP connections and I want
to create a perl object for each connection, and gather some data. The code
that I created is the following:
package Connection;
sub new {
my ($class) = @_;
my $self = {
_packet_ids=> {},
_bytes_per_second => [],
_source_node => undef,
_destination_node => undef
};
bless $self, $class;
return $self;
}
sub addToBytesPerSecond {
my ( $self, $currentBytesPerSecond) = @_;
my $atLocation = $self->{_seconds_counter};
$self->{_bytes_per_second}[$atLocation] = $currentBytesPerSecond;
print "";
}
sub printBytesPerSecond {
my ($self) = @_;
my $loopCounter = 0;
my $length = scalar($self->{_bytes_per_second});
while ($loopCounter < $length) {
print "$self->{bytes_per_second}[$loopCounter]\n";
$loopCounter++;
}
}
sub sourceNode {
my ( $self, $sourceNode ) = @_;
$self->{_source_node} = $sourceNode if defined($sourceNode);
return $self->{_source_node};
}
sub destinationNode {
my ( $self, $destinationNode) = @_;
$self->{_destination_node} = $destinationNode if defined($destinationNode);
return $self->{_destination_node};
}
The sourceNode and destinationNode functions work fine. But the
printBytesPerSecond and the addToBytesPerSecond functions do not work.
I can't understand what's wrong with them. Moreover, as you can see I also
want to have a hashtable with the packetIds that have been seen while
parsing, so as to filter duplicate packets. I didn't even attempt that,
because I couldn't understand how to insert a scalar into the hash, and how
to retrieve the value.
I would appreciate any help given.
G.
George Guest
-
Printing Array of Hashes
Hi John, I received your code. Thanks. I would like to know how to check the values of the keys in the hash. I checked the Perl cookbook and... -
How to store arrays in hashes or objects?
I had emailed this query out previously but since I never saw my own email in the digest, I'm assuming that it never made it to the... -
Slice an array of hashes?
Is it possible to slice a list of hashes? I read in data into the @data array as follows: while ( $i < $numLev ) { $line = <$fh>; ... -
adding to an array of hashes
When I run the following: for($k=0;$k<3;$k++){ $r->{'NUM'} = $k; #$temp->{'NUM'} = $k; #$r = $temp; print "added: $r->{'NUM'}\n"; } ... -
Comparing 2 hashes of array refs
On 11 Jul 2003, simo wrote: It's best to cut and paste from your code, as opposed to retyping. Hashes are defined by parenthesis, not by... -
James #2
Re: Help with array, hashes and objects
On Feb 16, 2004, at 2:46 PM, george wrote:
I'll provide some general comments below.
Is that one object per line/entry? That sounds like a lot to me.
Have you considered making a Log object that stores all the information
and provides summery information for it?
Just out of curiosity, why objects, over say a hash?
Please post some sample log data, so we can see what we're talking
about here. Help us help you. ;)
I like my constructors like this:
sub new {
my $class = shift;
my $self = {
_packet_ids=> {},
_bytes_per_second => [],
_source_node => undef,
_destination_node => undef,
@_
};
return bless $self, $class;
}
Which is the same as you have it, except I can override default
parameter choices at object creation. I do realize that you're using
the "_ Means Don't Touch" hint, so this may not be helpful in your
case. I'm pretty lax with object "security". I prefer functionality
and setting parameters in a constructor just makes sense to me.
I think we can simplify that a little. You just want to push an entry
onto that array, right?
sub addToBytesPerSecond {
my $self = shift;
push @{ $self->{_bytes_per_second} }, @_;
}
How's that? You can even add a whole list at once now.
Good rule of Perl thumb: If you're typing that much for something
simple, like an output loop, you're probably taking the hard road.
Easy things are easy in Perl.
sub printBytesPerSecond {
my $self = shift;
print "$_\n" foreach @{ $self->{_bytes_per_second} };
}
Using objects for everything... names like "sourceNode"... You a Java
guy? :D
Perl style for subroutine names is generally "source_node". We think
it reads a little better and is easier on our eyes. Just FYi. Use
whatever you like, of course.
Us Perl guys are also pretty allergic to extra variables we don't need.
We're weird that way. He's how we generally write the above:
sub source_node {
my $self = shift;
$self->{_source_node} = shift if @_;
return $self->{_source_node};
}
I'll let you trim that one, if you like.
See if that gets you going. If not, come back with sample data and
usage code and we'll get you straightened out.
Good luck.
James
James Guest



Reply With Quote

