puzzled by hashes and arrays

S

steve

Hi guys,
New to Perl and even newer to hashes, this is what I'm trying to
do......

I have different messages arriving at different interfaces, I want to
capture each message and save it away for later processing.
The messages are identified by the interface(IntFace) they arrived on,
the message id(MsgId), and the content of the message (a stream of hex
).

The attempts so far have been around a hash with a counter that saves
the IntFace, the MsgId and then I try to save the hex stream. On
retrieving the stream all I get in the number of bytes in the stream
rather than the actual contents.


$INCLUDE::mainConstants::RxMsgCounter++;

$INCLUDE::mainConstants::RxMessageHash{InterfaceId}[$INCLUDE::mainConstants::RxMsgCounter]
= $localIntfId;

$INCLUDE::mainConstants::RxMessageHash{MessageId}[$INCLUDE::mainConstants::RxMsgCounter]
= $localMsgId;

$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxMsgCounter]
= \@localFrame;

@localmsg =
$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxMsgCounter];
INCLUDE::Log::debugTrace "message array is - @localmsg";

Any help on how to do this so I get the hex back would be great

Regards

Steve
 
A

anno4000

steve said:
Hi guys,
New to Perl and even newer to hashes, this is what I'm trying to
do......

I have different messages arriving at different interfaces, I want to
capture each message and save it away for later processing.
The messages are identified by the interface(IntFace) they arrived on,
the message id(MsgId), and the content of the message (a stream of hex
).

The attempts so far have been around a hash with a counter that saves
the IntFace, the MsgId and then I try to save the hex stream. On
retrieving the stream all I get in the number of bytes in the stream
rather than the actual contents.

If you see the number of elements instead of the content of an array
the reason is probably that you have used the array in scalar context
along the way. But that situation doesn't fit with your code below.
$INCLUDE::mainConstants::RxMsgCounter++;

$INCLUDE::mainConstants::RxMessageHash{InterfaceId}[$INCLUDE::mainConstants::RxMsgCounter]
= $localIntfId;

$INCLUDE::mainConstants::RxMessageHash{MessageId}[$INCLUDE::mainConstants::RxMsgCounter]
= $localMsgId;

$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxMsgCounter]
= \@localFrame;

Here you assign an arrayref to, well, whatever. (As a side note, you
should find shorter ways to access that hash-of-arrays of yours.)
@localmsg =
$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxMsgCounter];

Here you assign the arrayref to @localmsg. That makes @localmsg an
array of one element, which is the arrayref. You'd have to de-reference
the array for the intended result:

@localmsg = @{ $INCLUDE::mainConstants... };
INCLUDE::Log::debugTrace "message array is - @localmsg";

That wouldn't print the number of elements in any list, it would print
a (stringified) arrayref, something like "ARRAY(0x814cc20)".

Anno
 
D

Dr.Ruud

steve schreef:
New to Perl and even newer to hashes, this is what I'm trying to
do......

I have different messages arriving at different interfaces, I want to
capture each message and save it away for later processing.
The messages are identified by the interface(IntFace) they arrived on,
the message id(MsgId), and the content of the message (a stream of hex
).

The attempts so far have been around a hash with a counter that saves
the IntFace, the MsgId and then I try to save the hex stream. On
retrieving the stream all I get in the number of bytes in the stream
rather than the actual contents.


$INCLUDE::mainConstants::RxMsgCounter++;

$INCLUDE::mainConstants::RxMessageHash{InterfaceId}[$INCLUDE::mainConsta
nts::RxMsgCounter]
= $localIntfId;

$INCLUDE::mainConstants::RxMessageHash{MessageId}[$INCLUDE::mainConstant
s::RxMsgCounter]
= $localMsgId;

$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxM
sgCounter]
= \@localFrame;

@localmsg =
$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxM
sgCounter];
INCLUDE::Log::debugTrace "message array is - @localmsg";

Any help on how to do this so I get the hex back would be great

It looks to me that you could use an array of hashes. See perldsc.

#!/usr/bin/perl
use warnings ;
use strict ;

use Data::Dumper ;

my @frame = (123, 'abc') ;

my @AoH ;
my $c = 0 ;

$AoH[$c]{'InterfaceID'} = 'eth0' ;
$AoH[$c]{'MessageId'} = '123.456@xyz' ;
$AoH[$c]{'Message'} = \@frame ;

print Dumper(\@AoH), "\n" ;

# wasteful copy
my @msg = @{ $AoH[$c]{'Message'} } ;

print join(', ', @msg), "\n" ;

But your Hash-of-Arrays could be the right structure too, it depends on
what you want to do with it.

An Array-of-Arrays structure is also feasible, if there is always the
same set of things to store about a message.

#!/usr/bin/perl
use warnings ;
use strict ;

use constant
{ IID => 0
, MID => 1
, MSG => 2
} ; # or use enum

use Data::Dumper ;

my @frame = (123, 'abc') ;

my @AoA ;
my $c = 0 ;

$AoA[$c] = [ 'eth0'
, '123.456@xyz'
, \@frame
] ;

print Dumper( \@AoA ), "\n" ;

# wasteful copy:
my @msg = @{ $AoA[ $c ][ MSG ] };

print join(', ', @msg), "\n" ;
 
S

steve

Dr.Ruud said:
steve schreef:
New to Perl and even newer to hashes, this is what I'm trying to
do......

I have different messages arriving at different interfaces, I want to
capture each message and save it away for later processing.
The messages are identified by the interface(IntFace) they arrived on,
the message id(MsgId), and the content of the message (a stream of hex
).

The attempts so far have been around a hash with a counter that saves
the IntFace, the MsgId and then I try to save the hex stream. On
retrieving the stream all I get in the number of bytes in the stream
rather than the actual contents.


$INCLUDE::mainConstants::RxMsgCounter++;

$INCLUDE::mainConstants::RxMessageHash{InterfaceId}[$INCLUDE::mainConsta
nts::RxMsgCounter]
= $localIntfId;

$INCLUDE::mainConstants::RxMessageHash{MessageId}[$INCLUDE::mainConstant
s::RxMsgCounter]
= $localMsgId;

$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxM
sgCounter]
= \@localFrame;

@localmsg =
$INCLUDE::mainConstants::RxMessage{Message}[$INCLUDE::mainConstants::RxM
sgCounter];
INCLUDE::Log::debugTrace "message array is - @localmsg";

Any help on how to do this so I get the hex back would be great

It looks to me that you could use an array of hashes. See perldsc.

#!/usr/bin/perl
use warnings ;
use strict ;

use Data::Dumper ;

my @frame = (123, 'abc') ;

my @AoH ;
my $c = 0 ;

$AoH[$c]{'InterfaceID'} = 'eth0' ;
$AoH[$c]{'MessageId'} = '123.456@xyz' ;
$AoH[$c]{'Message'} = \@frame ;

print Dumper(\@AoH), "\n" ;

# wasteful copy
my @msg = @{ $AoH[$c]{'Message'} } ;

print join(', ', @msg), "\n" ;

But your Hash-of-Arrays could be the right structure too, it depends on
what you want to do with it.

An Array-of-Arrays structure is also feasible, if there is always the
same set of things to store about a message.

#!/usr/bin/perl
use warnings ;
use strict ;

use constant
{ IID => 0
, MID => 1
, MSG => 2
} ; # or use enum

use Data::Dumper ;

my @frame = (123, 'abc') ;

my @AoA ;
my $c = 0 ;

$AoA[$c] = [ 'eth0'
, '123.456@xyz'
, \@frame
] ;

print Dumper( \@AoA ), "\n" ;

# wasteful copy:
my @msg = @{ $AoA[ $c ][ MSG ] };

print join(', ', @msg), "\n" ;

Dr.Ruud

Many Thanks, I've now got it working using your first example

Regards

Steve
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top