Hi, I am successfully 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", );
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:
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 );
I use these as follows:
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);
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:
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 ), );
Is this correct? How do I use it in my code, e.g. this doesn't work:
@packet{$%packet_fields{ $ident} } = unpack ( $packet_template{ $ident }, $buffer);
What should I use? Thanks, R.
-- Robin Bowes | http://robinbowes.com

In reply to Help with hash/array data structure by robinbowes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.