Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Sort never returns data in right order

by afoken (Chancellor)
on Sep 06, 2017 at 08:16 UTC ( [id://1198752]=note: print w/replies, xml ) Need Help??


in reply to Re: Sort never returns data in right order
in thread Sort never returns data in right order

Just a few comments on that piece of code:

# remove extra spaces my $fields = () = $summary =~ /[\s+,:]/g;

Misleading comment, misleading variable name. No space is removed from anywhere. It's just attempting to count the number of field separators, and it will probably fail. \s+ looks like you want to match any number of spaces, but that won't happen:

>perl -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new(q<[\s+, +:]>)->explain' The regular expression: (?-imsx:[\s+,:]) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- [\s+,:] any character of: whitespace (\n, \r, \t, \f, and " "), '+', ',', ':' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

And by the way: The split pattern does not fit the input.

elsif ( $fields == 10 ) { ( $Star, $Intf, $IHQ, $IQD, $OHQ, $OQD, $RXBS, $RXPS, $TXBS, $TXPS +, $TRTL ) = split( ' ', $summary );

$fields was calculated for a different set of field separators. Additionally, split on ' ' is special cased to emulate awk, see split.

This is overly complex. Just split the current line into an array, check if the array has the expexted number of fields (@array==10), and go on from there.

my $Star = 0; my $Intf = 0; my $IHQ = 0; my $IQD = 0; my $OHQ = 0; my $OQD = 0; my $RXBS = 0; my $RXPS = 0; my $TXBS = 0; my $TXPS = 0; my $TRTL = 0; my $Track = ""; # ... } elsif ( $fields == 10 ) { ( $Star, $Intf, $IHQ, $IQD, $OHQ, $OQD, $RXBS, $RXPS, $TXBS, $TXPS +, $TRTL ) = split( ' ', $summary ); # ... $Track = join "<<-", $rec, $Intf; $interface_bytes{$Track} += $RXBS; $Track = join "->>", $rec, $Intf; $interface_bytes{$Track} += $TXBS; # ... }

Scope of the variables should be limited to the block following elsif, i.e. my ($Star, $Intf, ...) = split .... Assigning unused fields to write-only variables is not needed, use undef instead: my ($x,undef,$y,undef,$z)=split .... Changing the code to split into an array instead of guessing field separators would require changes here, you would use just a constant index into an array. Readonly and constant could help avoiding magic numbers for the indexes, but on the other hand, you need those field numbers only here.

$Track = join "<<-", $rec, $Intf; $interface_bytes{$Track} += $RXBS;

join is overkill here. Just use string interpolation. That also gets rid of the $Track variable:

$interface_bytes{"$rec<<-$Intf"}+=$RXBS;
if ( $Star ne "*" ) { next; } elsif ( $RXBS =~ /\D/ ) { next; } elsif ( $TXBS =~ /\D/ ) { next; }

You check for errors, but you don't report them. Why?

Yes, I see that the heading line will trigger those errors. But why don't you get rid of the header line before working with the input?

} else { print STDERR "Danger Will Robinson - my sensors detect an invisible hole that may c +onsume you\n"; }

That perfectly explains the problem. For every f*ing line. Imagine reading in 10k lines from the wrong file. Seeing the same lame joke 10_000 times is not funny at all. If you find an unrecoverable error, just die, with a reasonable error message!


I'm sure I would find more things that I don't like if I would take some time to actually review the code. But to summarize:

Don't post bad code!

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: Sort never returns data in right order
by stall (Novice) on Sep 06, 2017 at 13:17 UTC
    I believe the previous person's post missed the point of my posts and does a disservice to the Perl community.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198752]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found