On 25/03/2011 02:36, Chris Stinemetz wrote: > Jim, > > I am getting really close to finishing up this program. perl is lot's > of fun and I appreciate all your help! > > The output is giving me the data I am looking for except for the > following issues: > > How would I add the correct code to make sure the array has a numeric > value before the loop iterations begin? > > I am getting the following error multiple times and when I look at the > output there are blanks. How can I eliminate this? > > Argument "" isn't numeric in addition (+) at ./DOband.pl line 30,<$fh> line > 3. > > Thank you in advance. > > #!/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 (<$fh>){ > 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 = sprintf "%.1f", > ( length( $dist )> 1 ) ? $dist/6.6/8/2*10/10 : 0 ; > > $sum{$cell}{$sect}{$carr}{$dist} += ($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}} > ) { > for my $dist ( sort keys > %{$sum{$cell}{$sect}{$carr}} ) { > push( @data, [ > $sum{$cell}{$sect}{$carr}{$dist}, $cell, $sect, $carr, $dist,]); > } > } > } > } > > for my $record ( sort { > $a->[1]<=> $b->[1] || > $a->[2]<=> $b->[2] || > $a->[3]<=> $b->[3] || > $a->[4]<=> $b->[4] } @data ) { > my( $val, $cell, $sect, $carr, $dist ) = > @$record; > print "$cell\t $sect\t $carr\t $dist\t $val\n"; > } >
Your error is because the $rlp variables can sometimes be empty strings as well as numeric values, in which case they should be skipped over. Also, since $carr is never used again after being extracted from $data[39], I would write things this way. my ($cell, $sect, $chan, $dist, $precis) = @data[31, 32, 38, 261, 262]; $dist = sprintf '%.1f', length $dist > 1 ? $dist / 6.6 / 8 / 2*10 / 10 : 0; my $carr = ( $cell < 299 && $chan == 175 ) || $chan == 1025 ? 2 : 1; for my $i (44..47) { my $rlp = $data[$i]; $sum{$cell}{$sect}{$carr}{$dist} += $rlp if $rlp; } HTH, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/