Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Correct syntax to access an array slice within a hash within a hash

by snowcrash (Friar)
on May 07, 2002 at 23:42 UTC ( [id://164855]=note: print w/replies, xml ) Need Help??


in reply to Correct syntax to access an array slice within a hash within a hash

Hi! Well, basically you want @{$table{$key}{coords}}[0,3] to get your slice.
you version ($table{$key}{coors}[0,3]) just uses the list 0,3 in a scalar context (i.e. the last element) and so only fetches the value indexed by 3.

a few other points:
  • you use the scalar variable $table in the beginning, later you just use the hash %table.
  • why do you set the value of each hash entry to the same integer as the key? you overwrite that later with a reference to another hash anyway.
  • most important point: use strict; and you would have noticed the other points i've addressed above ;)

    all in all i'd have done something like this:
    #!/usr/bin/perl -w use strict; my %table; for my $i (1..10) { $table{$i} = { shape => $i % 2 ? 'circle' : 'rect', coords => [0,1,2,3,4,5,6,7,8,9] } } foreach my $key (keys %table) { print "Key $key Value $table{$key}\n", "Shape $table{$key}{shape}\n", join(",", @{$table{$key}{coords}}[0,3]), "\n"; }

    snowcrash
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: note [id://164855]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others sharing their wisdom with the Monastery: (12)
    As of 2024-04-16 07:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found