There are a couple things going on that will make this all a bit bettr, I think.

First off, using the "-w" switch and "use warnings" is redundant. That's just a minor nit.

It would be common to do the enumeration of the %saldi hash outside of the while() loop. The while loop will exit when there is no further data to process. At that point you'll know you've reached EOF, so there's no need to test for it as you are there.

It's also a good practice to test for the validity of the incoming data before you try to do anything significant with it. So, a check of the incoming data along with a check of the @cellen array after the split will keep you from trying to do things with bogus data. Here's just a quick example of what I'm talking about:

#!/usr/bin/perl use strict; use warnings; use diagnostics; my %saldi; while (<>) { chomp; next unless $_; my @cellen = ( split /,/, )[ 3, 4 ]; next unless $cellen[0] && $cellen[1]; $saldi{ $cellen[0] } += $cellen[1]; } $ARGV =~ m/^(\S+)\.txt/; print "$1\n"; foreach my $name ( keys %saldi ) { print "\t$name\t$saldi{$name}\n"; }

In reply to Re: Split pattern doesn't match last line of file by SheridanCat
in thread Split pattern doesn't match last line of file by GertMT

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.