robinbowes has asked for the wisdom of the Perl Monks concerning the following question:
So far so good. I can read an ident from a file and then use construct like $packet_template{ $ident } I am also using the following data structures:# List all packet types that this script knows about my %Idents = ( Rotation_Marker => 0x90, Format_7 => 0x87, ); # List packet lengths of each of the packet types (Ident char to check +sum, inclusive) my %Packet_Length = ( $Idents{ Rotation_Marker } => 30, $Idents{ Format_7 } => 28, ); # unpack templates to use for each packet type (not including the pack +et Ident) my %packet_template = ( $Idents{ Rotation_Marker } => "C3 n n n n n n n n n n n n C3", $Idents{ Format_7 } => "C B16 n2 A4 A8 A2 A A A3 C2", );
I use these as follows:my @packet_fields = qw( ident flags x y mode3 callsign route spare1 spare2 flight_level terminator chksum ); my @rotation_packet_fields = qw(ident RDP_Ident Radar_Site_Ident site_x site_y Useful_Vol_Upper Useful_Vol_Lower Useful_Vol_North Useful_Vol_South Useful_Vol_East Useful_Vol_West Rotation Rotation_x Rotation_y Reserved QNH_Datum terminator chksum );
Now, what I'd like to do is to create a hash of these arrays and refer to them in a similar fashion to the way I refer to the %packet_template hash. However, I can't work out how to do it. I've come up with the following declaration to create the hash, but I don't know how to re-reference it:if ($ident == $Idents{ Rotation_Marker }) { @packet{ @rotation_packet_fields } = unpack ( $packet_template{ $ident }, $buffer); ... if ($ident == $Idents{ Format_7 }) { @packet{ @packet_fields } = unpack ( $packet_template{ $ident }, $buffer);
my %packet_fields = ( $Idents{ Format_7 } => qw( ident flags x y mode3 callsign route spare1 spare2 flight_level terminator chksum ), $Idents{ Format_7 } => qw(ident RDP_Ident Radar_Site_Ident site_x site_y Useful_Vol_Upper Useful_Vol_Lower Useful_Vol_North Useful_Vol_South Useful_Vol_East Useful_Vol_West Rotation Rotation_x Rotation_y Reserved QNH_Datum terminator chksum ), );
What should I use? Thanks, R.@packet{$%packet_fields{ $ident} } = unpack ( $packet_template{ $ident }, $buffer);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with hash/array data structure
by princepawn (Parson) on Nov 11, 2003 at 17:24 UTC | |
|
Re: Help with hash/array data structure
by injunjoel (Priest) on Nov 11, 2003 at 18:39 UTC | |
|
Re: Help with hash/array data structure
by shockme (Chaplain) on Nov 11, 2003 at 17:23 UTC | |
|
Re: Help with hash/array data structure
by duff (Parson) on Nov 11, 2003 at 18:17 UTC | |
|
Re: Help with hash/array data structure
by Roy Johnson (Monsignor) on Nov 11, 2003 at 17:45 UTC | |
|
Re: Help with hash/array data structure
by Art_XIV (Hermit) on Nov 11, 2003 at 18:48 UTC | |
by robinbowes (Beadle) on Nov 13, 2003 at 10:07 UTC |