velocitymodel has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I am having trouble parsing my records and then sorting them. When sorting my records I get a an error at line 22 in my code. It looks like this.

use strict; use warnings; my $fin = "SELECTDAT2"; my $fout = "myfile"; open my $ih, '<', $fin or die "cannot open $fin for reading, $! "; open my $oh, '>', $fout or die "cannot open $fout for writing, $! "; my @records; my @list; while (<$ih>) { my @tokens = split; my $SEC1 = $tokens[3]; my $LAT = $tokens[4]; my $LONG = $tokens[5]; my $DEPTH = $tokens[6]; my $NO = $tokens[10]; my $GAP = $tokens[11]; for $_ (@list) { my ($SEC1, $LAT, $LONG, $DEPTH, $NO, $GAP) = parse_record($_); push @records, [ $SEC1, $LAT, $LONG, $DEPTH, $NO, $GAP ]; } #sort by LONG (index 0=NO, 1=SEC1, 2=LONG, 3=LAT, 4=DEPTH, 5=GAP) my $nextline = <$ih>; $records = \@records; my @sorted_recs = sort { $records {$a} <=> $records {$b} } @records; my $y = $tokens[0], my $m = $tokens[1], my $d = $tokens[2], my $h = $t +okens[3]; sub events { my (@list); my $nextline = <$ih>; foreach $nextline (@list) { my @tokens = split; my $SOURCE = $tokens[0]; my $PSEC = $tokens[3]; my $PQ = $tokens[4]; my $SSEC = $tokens[7]; my $SQ = $tokens[8]; last if $nextline eq ""; push (@list, $_); } return @list; } last if eof(); for my $rec (@sorted_recs) { my ($NO, $SEC1, $LONG, $LAT, $DEPTH, $GAP) = @$rec; print $oh "$NO\n", events(@list), "\n", "$SEC1, $LONG, $LAT, $DEPTH, $ +GAP\n"; } } close ($oh); close ($ih);Global symbol "$records" requires explicit package name at + - line 22. Global symbol "$records" requires explicit package name at - line 22.

I was advised to use $$records($a)(2) <=> $$records($b)(2) and I am having trouble understanding the use of $$records and how to correct this error. I would really appreciate some advice and help.

Replies are listed 'Best First'.
Re: Parse variables
by ikegami (Patriarch) on Feb 25, 2011 at 06:06 UTC
    my @sorted_recs = sort { $$records[$a][2] <=> $$records[$b][2] } @reco +rds;

    You're treating $records as a reference to an array, yet you don't even have such a variable.

    $a and $b are aliased to the two elements of @records that need to be compared. The elements of @records are references to records (arrays of 6 fields).

    my @sorted_recs = sort { $a->[2] <=> $b->[2] } @records;
Re: Parse variables
by jethro (Monsignor) on Feb 25, 2011 at 10:15 UTC
    use v5.10.0; use warnings; @records=('nil','one','two'); $records= 50; say $records[1]; #prints the string one say $$records; # error, because $records doesn't point anywhere say $$records[0]; #still error, because $records doesn't point anywhere #you are not accessing @records here at all $records= \@records; #now $records is a reference to the array @records, but we could have #used a different variable name as well, for example $x say $$records[1]; #prints 'one', because $records points to @records $records= \$records[1]; say $$records; #prints 'one', because $records points to $records[1] $a= \$records[1]; $b= \$records[2]; #this is how sort prepares the variables $a and $b if it wants to comp +are those two records say $$a,$$b; #prints 'onetwo'
Re: Parse variables
by umasuresh (Hermit) on Feb 25, 2011 at 13:07 UTC
    Additionally use Devedl-ptkdb to debug your code.
    This will save you from coding related heartburn, headache... If you are running perl on windows it is very easy to install this module.

    At the command prompt:
    ppm install Tk (which is required to run the debug tool) ... ppm install Devel-ptkdb
    Invoke the debug tool by typying:
    perl -d:ptkdb my_script.pl