Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Help Me!

by jwkrahn (Abbot)
on Apr 10, 2011 at 04:23 UTC ( [id://898563]=note: print w/replies, xml ) Need Help??


in reply to Help Me!

#!/usr/bin/perl use strict; use warnings;

Very good start!    These pragmas should be at the beginning of every Perl program.    (Note that it is spelled Perl, not PERL.)



open FILE, "/home/ajb004/Paradigms/roster.txt" or die $!;

You should usually use the three argument form of open and lexically scoped filehandles and you should probably include the file name in the error message so you know which file failed to open.



$_;

You have a variable in void context which should trigger a warning message.



$_ = $word; s/_/ /g; print "ID: $_\n";

Why are you modifying $_ in this loop when you could just use $word directly?

$word =~ s/_/ /g; print "ID: $word\n";

And since you are also using $_ in the outer loop then this could cause problems in some situations so you should probably avoid using $_ if you can:

while ( my $line = <FILE> ) { my @words = split /,/, $line; foreach my $word ( @words ) { $word =~ s/_/ /g; print "ID: $word\n"; } }

And if you are modifying single characters then the tr/// operator is usually more efficient than the s///g operator.



According to your explanation and data you probably need something like this:

#!/usr/bin/perl use strict; use warnings; my $file = "/home/ajb004/Paradigms/roster.txt"; open my $FILE, '<', $file or die "Cannot open '$file' because: $!"; while ( my $line = <$FILE> ) { chomp $line; $line =~ tr/_/ /; my ( $id, $name, $major, $email ) = split /,/, $line; print <<TEXT; ID: $id Name: $name Major: $major Email: $email TEXT }

Replies are listed 'Best First'.
Re^2: Help Me!
by JavaFan (Canon) on Apr 10, 2011 at 06:03 UTC
    You should usually use the three argument form of open and lexically scoped filehandles and you should probably include the file name in the error message so you know which file failed to open.
    Actually, it's quite common that it doesn't matter whether one uses 2-arg, or 3-arg open. This is one of the cases.

    As for the error message, considering the program only tries to open one file, with a fixed name, it should be bloody obvious which file failed to open. "Should include the file name" is a much too strong.

      The sooner one starts using safety nets like strictures, lexically scoped variables, full error messages and so on the more benefit one derives from their use and the more likely that such techniques will become habit. The time when such techniques are most useful is during the discovery stages of learning the language, not later on when one should know better in any case and has a better debugging toolkit to help sort problems out in any case.

      While there is a balance between introducing too much and introducing good habits early, the OP (by accident or design) is already using strictures and checking the success of open, so introducing another related three safety net techniques seems reasonable.

      True laziness is hard work

Log In?
Username:
Password:

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

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

    No recent polls found