On 08/09/2011 20:58, Chris Stinemetz wrote:
What would be the best way to omit any record when varible $dist is
null or not>  1 ?

I am not sure my attempt is correct with the ternary operator with
length function. It seems that $dist is just being assinged 0 when
the expression $dist is not>  1.

Any help is greatly appreciated.

Thank you,

Chris

#!/usr/bin/perl

use warnings;
use strict;
use POSIX;

my $filepath = sprintf("F:/Backup/MySQL/10072810.EVDOPCMD");
my $runTime =  sprintf("C:/%s.txt",strftime("%y%m%d%H%M",localtime));
#my $fileDate = strftime("%y%m%d%H%",localtime);

open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
open my $out, '>', $runTime or die "ERROR opening $runTime: $!";

my %sum;

while (<$fh>){
     next unless /;/;
        chomp;
        my @data = split /;/;
        my($cell,$sect,$chan,$rlp1,$rlp2,$rlp3,$rlp4,$dist) =
          @data[31,32,38,44,45,46,47,261];

                if( $cell>= 1&&  $cell<= 900&&  $sect>= 1&&  $sect<= 6 ) {  

                        my $carr =      
                                ( $cell<= 299&&  $chan == 175 )              ? 
2 :
                                ( $cell>= 300&&  $cell<= 599&&  $chan == 75 ) ? 
2 :
                                ( $chan == 1025 )                               
? 2 : 1 ;
                
                        $dist = sprintf "%.1f", ( length( $dist )>  1 ) ? 
$dist/8/6.6/2 : 0;
                
                
                for my $i (44..47) {
                my $rlp = $data[$i];
                $sum{$cell}{$sect}{$carr}{$dist} += $rlp if $rlp;
                }
        }       
}

my @data;
my $lastDist = 0.0;
        for my $cell ( sort keys %sum )
        {
                for my $sect ( sort keys %{$sum{$cell}} )
                {
                        for my $carr ( sort keys %{$sum{$cell}{$sect}} )
                        {
                        $lastDist= 0.0;
                        for my $dist ( sort keys %{$sum{$cell}{$sect}{$carr}} )
                        {
                                for(my $i = $lastDist * 10; $i<  $dist * 10; 
$i++)
                                {
                                        push( @data, [ 0, $cell, $sect, $carr, 
sprintf("%0.2f",$i/10),]);
                                }
                                $lastDist=$dist+.1;
                                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 $out "$cell\t $sect\t $carr\t $dist\t $val\n";
     }
     close $out;

Hi Chris

You write

What would be the best way to omit any record when varible $dist is
null or not>  1 ?

But from your code, I assume you mean to omit records when the _length_
of $dist isn't greater than one?

I would make the check immediately $dest is assigned, and 'next' over
the record in the same way as you do for those that contain no semicolon.

  my($cell,$sect,$chan,$rlp1,$rlp2,$rlp3,$rlp4,$dist) =
      @data[31,32,38,44,45,46,47,261];

  next unless length $data > 1;

and then later on, you know $dist is valid so there is no need for the
test when you are reformatting it:

  $dist = sprintf "%.1f", $dist/8/6.6/2;

HTH,

Rob

--
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