#! /usr/bin/perl use strict; use warnings; use constant CLIENT_NICK => 'ClientNick'; use constant CLIENT_PATH => 'ClientPath'; my @array; for(my $n = 0; $n < 10; $n++) { my %result = ( CLIENT_NICK => "NICK: $n", CLIENT_PATH => "PATH: $n", ); push @array, %result; } foreach (@array) { print $_->{CLIENT_NICK} . "\n"; print $_->{CLIENT_PATH} . "\n"; }

This results in the error: "Can't use string ("CLIENT_PATH") as a HASH ref while 'strict refs' in use at ./main.pl line 23." Apart from the fact that what I'm 'pushing' into the @array is not a reference (and the curios fact that it complains over CLIENT_PATH before CLIENT_NICK) I'm stumped as to how to get at my data. if I simply

foreach (@array) { print "$_\n"; }

It outputs my data nicely as

CLIENT_PATH Path: 0 CLIENT_NICK Nick: 0 CLIENT_PATH Path: 1 CLIENT_NICK Nick: 1 ... etc

(again with the CLIENT_PATH before the CLIENT_NICK). Can someone explain this to me and tell me how I get at my data in an orderly fashion along the lines of the first example, please? TIA /Soren


In reply to Accessing HASH pushed into @array ('strict refs' in use error) by sbrothy

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.