Doug Cacialli wrote:

$datapath =~ s/^\s+//;
$datapath =~ s/\s+$//;

Alternative notation:

    s/\s+$//, s/^\s+// for $datapath;

I have a "sub trim()" in my Toolbox, so I just call "trim($datapath);".


open (my $filehndl , "<", "$datapath") ||
        die ("Can't open .txt file $datapath. Exiting program.\n\n");

Why quote $datapath?
(It isn't in a class with overloaded stringification.)

    open my $filehndl , "<", $datapath or die "'$datapath': $!\n";


                while (my $line = <$filehdl2>)
                {
                        chomp $line;
                        my @words = split / /, $line;
                        my $nr_words = @words;
                        print "\n$line\n";
>                    print "The line above has " . scalar @words ...

If you wouldn't chomp, you wouldn't have to add a "\n".
But then you need to split on " " (to get rid of empty trailings).


    while (my $line = <$fh>) {
        my @words = split " ", $line;
        print $line, "\thas ", scalar(@words), " words.\n";
    }


--
Ruud

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