>>>>> "CS" == Chris Stinemetz <chrisstinem...@gmail.com> writes:

  CS> I would like to make an adjustment to this ternary operator.
  CS> Instead of returning 0 if length( $dist ) is not > 1. I would like to
  CS> return the last $dist value incremented by 0.1 mile so there is no gap
  CS> of more than 0.1 miles.

one point i want to make to you (if you would listen) is you need to
isolate logical perl things from each other. you don't modify a ternary
operator. you change the expressions it uses. you can say you want to
modify the conditional expression. in a earlier post it seemed like you
tied the conditional expression operator to the expressions it was
using. they are independent as are all perl expressions from their
operators. a classic version of this is when newbies talk about the <<
option to print (<< is a here document which is commonly used but
independent from print). i am telling this to you because clarifying how
you write and talk about perl (or any language) will help you in
describing problems accurately and logically which always helps in
solving them.

  CS> my $filepath = '/home/cstinemetz/perl_programs/1.EVDOPCMD';
  CS> my $runTime =  
sprintf("/cygdrive/c/temp/%s.txt",strftime("%y%m%d%H%M",localtime));
  CS> my $fileDate = strftime("%y%m%d%H%",localtime);



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

that should say 'creating' $runTime as you are writing to it. you get
different errors when opening for reading (e.g. 'nonexisting file) vs
writing (e.g. no permission to write a file).

  CS> my $time = localtime;

why do you get the time again? call localtime once and use the $time var
for the strftime call.

  CS> my %sum;

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

again, use better names. anytime you see vars with integer suffixes,
think array. @rpl can hold the 4 values. if you want that at the end
(since arrays will slurp all the rest of the elements) you can reorder
the slice indices:

        my( $cell, $sect, $chan, $dist, @rlp ) =
                @data[31, 32, 38, 261, 44 .. 47];

i also put spaces in there and made the @rlp indicies a range which is
easier to read and check.


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

  CS>           my $carr =      

why is that not indented from the preceding if block? always indent inside a
new block.

  CS>                   ( $cell <= 299 && $chan == 175 )                        
        ? 2 :
  CS>                   ( $cell >= 300 && $cell <= 599 && $chan == 75 )
  CS>                     ? 2 :
  CS>                   ( $chan == 1025 )                                       
        ? 2 : 1 ; #nested ternary operator      

useless comment. it is obvious what that is.
                
  CS>           $dist = sprintf "%.1f",
  CS>                   ( length( $dist ) > 1 ) ? $dist/6.6/8/2*10/10 : 0 ; 
##### would
  CS> like to return increment of 0.1 if $dist < 1 #######

stop putting comments on the line of code. it make the line wrap even
more (fix the wrapping too) and makes it hard to read.
                
  CS>           for my $i (44..47) {
  CS>           my $rlp = $data[$i];
  CS>           $sum{$cell}{$sect}{$carr}{$dist} += $rlp if $rlp;

you already have the rlp values. why do you get them again? and if they
are in the array as i showed, it is even easier to get them.

                for my $rlp ( @rpl ) {
                        $sum{$cell}{$sect}{$carr}{$dist} += $rlp if $rlp;
                }

no need for $i (which i hate to see unless absolutely necessary).


  CS>           }
  CS>   }       
  CS> }
        
  CS>   my @data;
  CS>           for my $cell ( sort keys %sum ) {
  CS>                   for my $sect ( sort keys %{$sum{$cell}} ) {
  CS>                           for my $carr ( sort keys %{$sum{$cell}{$sect}} 
) {
  CS>                                   for my $dist ( sort keys 
%{$sum{$cell}{$sect}{$carr}} ) {

that is highly ineffcient, indexing deep into the tree over and over at
each level. get the reference to the current level and then index into
that. it will be cleaner, faster and better. (untested)


        for my $cell ( sort keys %sum ) {

                my $top_sect = $sum{$cell} ;
                for my $sect ( sort keys %{$top_sect} ) {

                        my $top_carr = $top_sect->{$sect} ;
                        for my $carr ( sort keys %{$top_carr} ) {

etc. etc.

the names may not be the best as i don't understand the data here. the
concept is fairly clear - get a reference to the current level and use
that instead of deep indexing at each level. there is plenty of docs on
this: perldsc, perllol, perlreftut.


  CS>   for my $record ( sort {
  CS>                                 $a->[1] <=> $b->[1] ||
  CS>                                 $a->[2] <=> $b->[2] ||
  CS>                                 $a->[3] <=> $b->[3] ||
  CS>           $a->[4] <=> $b->[4] } @data ) {

might as well align the 4th row too. looping over the actual sort
expression is a clunky looking style. sorting to an array and then
looping is much better.

and here you can indent less to make it easier to read:

        my @sorted = sort {
                $a->[1] <=> $b->[1] ||
                $a->[2] <=> $b->[2] ||
                $a->[3] <=> $b->[3] ||
                $a->[4] <=> $b->[4] } @data ;

        foreach my $record ( @sorted ) {

etc.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

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