Karthick S
Karthick S

Reputation: 3304

Sorting a perl array of arrays

I have a table in an array and I am trying to sort it using the following method:

@table_array = sort { $a->[0] <=> $b->[0] } @table_array;

But I get this error:

Can't use string ("5") as an ARRAY ref while "strict refs" in use at 

My $table_array[x][0] is a numeric value. I create this table by reading two files and merging them on a common field ($table_array[x][1] to be exact).

I am not able to understand where I have asked the elements to be accessed using string references.

Thanks, Karthick S.

Upvotes: 0

Views: 244

Answers (1)

ikegami
ikegami

Reputation: 385976

My $table_array[x][0] is a numeric value.

No, that's not true. For one of the elements, $table_array[x] is 5 instead of a reference.

>perl -e"use strict; $a=5; $a->[0]"
Can't use string ("5") as an ARRAY ref while "strict refs" in use at -e line 1.

Maybe you did

$table_array[$x] = @rec;

instead of

$table_array[$x] = \@rec;

Upvotes: 3

Related Questions