I've not been able to reproduce that error message. Generally that sort of message comes up when you do something like this:
for my $x ('x') { $x =~ s/x/X/ }
That is, when you have an alias to a literal, and you try to modify it. You can see a related error message like this:
'x' =~ s/x/X/;
Here are a few things wrong with your code...
When you're setting variables within a loop, it's almost always a good idea to make them lexical variables scoped to the loop. (i.e. use my.) The use strict 'vars' pragma forces you to declare all variables (except Perl's pre-defined ones like $_) so unless you're writing a 3 line script is always a good idea.
One of the nice things about $_ is that it's implicit in so many places. Your first couple of lines within the loop could be written as:
chomp; my @line_array = split /[()\t+\s+]/;
This:
@full = map {$_ ? $_ : ()} @line_array;
would be more natural with grep. Use grep to filter a list, and map to transform it.
@full = grep { $_ } @line_array;
... though actually this is probably filtering out more things than you want. You probably don't want to be removing numeral "0" from the list. So...
@full = grep { defined $_ and length $_ } @line_array;
This is not doing what you think it's doing:
@{$data[$readcounter]} = @full;
Besides which, $readcounter doesn't seem to be defined or incremented anywhere. What you probably want is:
push @data, \@full;
Putting all this together, you get:
use strict; use Data::Dumper; my @data; while (<DATA>) { chomp; my @line = grep { defined $_ and length $_ } split /[()\t+\s+]/; push @data, \@line; } @data = sort { $a->[0] <=> $b->[0] } @data; print Dumper \@data; __DATA__ 00000(IDR) 86480 22 41.435 40.696 40.728167 0 FRM 3 00015( B ) 9312 24 45.460 43.808 42.001 409 208 FRM 0 00002( P ) 35248 24 38.568 39.327 40.641 253 53 FRM 2
I'd in fact go further and use Sort::Key to make that sort look a little nicer...
use strict; use Data::Dumper; use Sort::Key qw(nkeysort_inplace); my @data; while (<DATA>) { chomp; my @line = grep { defined $_ and length $_ } split /[()\t+\s+]/; push @data, \@line; } nkeysort_inplace { $_->[0] } @data; print Dumper \@data; __DATA__ 00000(IDR) 86480 22 41.435 40.696 40.728167 0 FRM 3 00015( B ) 9312 24 45.460 43.808 42.001 409 208 FRM 0 00002( P ) 35248 24 38.568 39.327 40.641 253 53 FRM 2
In reply to Re: Read Only Error -- Sorting an Array
by tobyink
in thread Read Only Error -- Sorting an Array
by perlstudent89
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |