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 | |
by GrandFather (Saint) on Apr 10, 2011 at 07:51 UTC |