Array of hashes and strict refs - help!

From: Noel Sant (noel.sant_at_ntlworld.com)
Date: 01/30/04


Date: Fri, 30 Jan 2004 16:43:48 -0000

When I run my (test) program below, I get the following error:

Can't use string ("file") as a HASH ref while "strict refs" in use at
D:\Houseke
eping\BackupWithZip\Scripts\hashtest.pl line 28, <INPUT_FILE> line 2.

The camel book says this means only hard references are allowed by "use
strict refs", not symbolic ones. But this is more or less what the camel
book (2nd edition) says to do with arrays of hashes (pp268/9). Why is "file"
a symbolic ref? Where am I going wrong?

TIA

    Noel

# Work out how to use an array of hashes with strict refs
#
 use strict;

 my $input_line; my $folder; my $file;
 my @array; my %hash;
 my $key; my $i;

# Lines in test.txt have two strings: a folder name and a file name,
# separated by spaces
   open INPUT_FILE, "test.txt"
      or die "Can't open test.txt\n";
   while (!eof INPUT_FILE) {
      $folder = "";
      $file = "";
  chomp($input_line = <INPUT_FILE>);
# Lots of checking of input line for double quotes and spaces,
# but with a simple line ("aaa bbb") ...
  ($folder, $file) = split (/\s+/, $input_line);
  %hash = (
   folder => $folder,
   file => $file,
  );
  push @array, %hash;
 }
 %hash = ();
 foreach $i (@array) {
  for $key (keys %{$array[$i]}) { # <=== line 28
   print "$key name is $array[$i]{$key}\n";
  }
 }
 close INPUT_FILE;



Relevant Pages

  • Re: Array of hashes and strict refs - help!
    ... > The camel book says this means only hard references are allowed by "use ... > strict refs", not symbolic ones. ... You can't push a hash into an array. ...
    (comp.lang.perl.misc)
  • Re: No destructive modification of array in while loop [wasRe: Trim whitespace with cookbook...]
    ... afternoon in the Camel book trying to understand and test some things. ... used a string, it was faster than any modification I would've done, plus ... # Takes $string and throws it into an array of ASCII values" ... 1: Then we turned, and took our journey into the wilderness by the way of the Red sea, as the LORD spake unto me: and we compassed mount Seir many days. ...
    (comp.lang.perl.misc)
  • Re: Array size
    ... > looked at all the $# hits, ... > The special variable $#array tells you the index of the last ele- ... page 76 in the Camel book. ... HTH, ...
    (comp.lang.perl.misc)