1
votes

First off, I know this is a fairly common issue but I have looked around and can't quite seem to pinpoint why its happening to me!

I have some data I am reading from a JSON file, basically all it is are a set of string that represent IDs.

I import it like so:

 my $idFile='IDS.json';
 my $idJSON;



 {
         local $/;
         open my $fh, '<', $idFile or die $!;
         $idJSON = <$fh>;
         close $fh;
 }

 my $id_array = decode_json $idJSON;

This is what $id_array looks like now:

$VAR1 = [
          '3233',
          '2758',
          '2797'
        ];

I then save them with a MISC tag in the form of a hash map, but this is where my "Can't use string ("3233") as a HASH ref while "strict refs" in use " error is being thrown:

my @decodedIDS = map { $_ ->{MISC}} @{$id_array};

Anyone have suggestions as to what is causing this error? Any help is greatly appreciated, as always.

1
I'm unclear what you're trying to do...can you please explain what you want @decodedIDS to contain? - ThisSuitIsBlackNot
"I then save them with a MISC tag in the form of a hash map" ... then maybe you meant to say @decodedIDS = map { MISC => $_ } @{$id_array}? - mob
That's not a hash, @mob. my %decodedIDS = map { $_ => 1 } @$id_array; would create a hash keyed by id. - ikegami

1 Answers

2
votes

I don't understand what you're trying to do but below is the reason of the error.

map { $_->{MISC} } @{$id_array}

means take each element from given dereferenced arrayref and access the MISC key of that element (it is expecting the element to be a hashref). In your case the element is literal string (3233 for example). So it kinda looks like this:

3233->{MISC}

Therefore you get below error:

"Can't use string ("3233") as a HASH ref while "strict refs" in use