On 3/24/11 Thu  Mar 24, 2011  9:04 AM, "Chris Stinemetz"
<cstinem...@cricketcommunications.com> scribbled:

> I would like $dist be part of the print statement, but I am not sure how to
> code it correctly.

The value of $dist will vary for each row. If you want to print out the
correct value of $dist that corresponds to the value of the sum, then you
will have to store the value.

> 
> I am getting the following error:
> 
> 
> Global symbol "@dist" requires explicit package name at ./jim.pl line 38.
> Execution of ./banding aborted due to compilation errors.

See note below line 38.

> 
> 
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> #my $filepath = 'C:/temp/PCMD';
> # my $filepath = '/home/cstinemetz/perl_programs/1.EVDOPCMD';
> # my $outfile  = 'output.txt';
> 
> # open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
> # open my $out, '>', $outfile or die "ERROR opening $outfile: $!";
> 
> my %sum;
> 
> while (<DATA>){
>     next unless /;/;
> chomp;
> my @data = split /;/;
> my($cell,$sect,$chan,$carr,$rlp1,$rlp2,$rlp3,$rlp4,$dist,$precis) =
>       @data[31,32,38,39,44,45,46,47,261,262];
> 
> $carr = 
> ( $cell < 299 && $chan == 175 )  ? 2 :
> ( $chan == 1025 )      ? 2 : 1 ; #nested ternary operator 
> 
> $dist = 
> ( length( $dist ) > 1 ) ? $dist/6.6/8/2*10/10 : 0 ;
>  
>  
> $sum{$cell}{$sect}{$carr} += $rlp1 += $rlp2 += $rlp3 += $rlp4 || 0 ; }

I see you are changing your program requirements. You are now accumulating
multiple rlp values instead of one.

This line has two problems. The first is that you have placed the closing
brace at the end of the line where it is easy to lose. Perhaps this is why
you have mistakenly used the $dist variable in the lines below, where it is
not in scope. The second problem is the multiple '+=' operators. Do you know
what they will do in this case? I don't. I would have to run a test to see
what the effect is on the intermediate variables. You might be doing more
arithmetic than is necessary.

If you want to accumulate the four variables, why not just do:

$sum{$cell}{$sect}{$carr} += ($rlp1 + $rlp2 + $rlp3 + $rlp4) || 0 ;

> my @data;
> for my $cell ( sort keys %sum ) {
> for my $sect ( sort keys %{$sum{$cell}} ) {
> for my $carr ( sort keys %{$sum{$cell}{$sect}} ) {
> push( @data, [$dist[ $sum{$cell}{$sect}{$carr}, $cell, $sect, $carr ]]);

The syntax "$dist[ ... ]" tells the Perl parser to access an element of the
array @dist. The proper way to include $dist with the other values would be:

push( @data, [$sum{$cell}{$sect}{$carr}, $cell, $sect, $carr, $dist ] );

but if you use that, you will get a compiler error, as $dist is not in scope
here. You need to store the value of $dist when you read each record, and
retrieve it before using it here.

You will have to figure out how to save and retrieve the value of dist for
each record. There will be one dist value for each record, but the value of
the sum will be the sum of several records, so I think you have inconsistent
logic in what you are trying to do. Which of the several dist values do you
want printed with the sum values?

> }
> }
> }
> 
> for my $record ( sort {
> $a->[1] <=> $b->[1] ||
> $a->[2] <=> $b->[2] ||
> $a->[3] <=> $b->[3] } @data ) {
> my( $val, $cell, $sect, $carr, $dist ) = @$record;

This line is good, but you did not store your values this way. See above.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to