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

    In reply to Re: Correct syntax to access an array slice within a hash within a hash by snowcrash
    in thread Correct syntax to access an array slice within a hash within a hash by Popcorn Dave

    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.