sbrothy has asked for the wisdom of the Perl Monks concerning the following question:
#! /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
Back to
Seekers of Perl Wisdom