user3049982
user3049982

Reputation: 119

Type of arg 1 to keys must be hash (not private array) near "@array)"

Get following error while attempting to run below code.

Type of arg 1 to keys must be hash (not private array) near "@array)"

The idea of the code is compare two arrays data. This method works on my home server, but will not work on the server it needs implemented on.

Not sure where to go with it.

 foreach (sort keys @array){
        unless (exists $group_list[$_]){
            print "$_: not found\n";
            next;
        }
        if (equivalent($array[$_],$group_list[$_])){
            print "$_: values are equal\n"
        }else{
            print "$_: values are not equal\n";
        }
    }

Please let me know if more information is needed.

Upvotes: 0

Views: 1421

Answers (1)

amon
amon

Reputation: 57600

keys @array only works on newer perls, and should therefore generally be avoided. We can write down the range of indices directly without much extra syntax: Unless $[ has been set, indices start with 0 and are a continuous range up to $#array, the last index. Then:

for (sort 0 .. $#array)

Note that this range is already sorted numerically, and sort will sort it alphabetically. Remove the sort if that behavior is not desired:

for (0 .. $#array)

That is the normal idiom for iterating over all indices.

Note also that (unless you do highly unusual things), exists $array[$_] will probably be true, so this test is not likely to be useful (it can be false e.g. if you preextended the array to a specific size: $#array = $size - 1). Testing whether an entry is defined is more likely to be useful.

Upvotes: 3

Related Questions