Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Help with split() function

by hipowls (Curate)
on Mar 19, 2008 at 10:19 UTC ( #674971=note: print w/ replies, xml ) Need Help??


in reply to Help with split() function

As others have pointed out your code is very wrong in places. I'll go over it line by line

#!usr/bin/env perl use warnings; use strict;
so far so good

Now it starts to go awry

open my $test_fh, '<', @ARGV or die "Can not open file $!\n";
If your program is invoked with no arguments it will exit with the error Can not open file No such file or directory and if it has two or more arguments it will exit with More than one argument to open(,':perlio') at ./Perl-1.pl line 11.. Neither error is helpful to the user. You should
  • Check the number of arguments and exit with a helpful usage message
  • Put the name of the file that couldn't be opened in the die message.
Marks for $!.

while (<$test_fh>) {
OK although many people would explicitly assign <test_fh> to a lexical variable.

Now we get to the real problems

my @line = split (); # if ($line[1] =~ /\wP\w/i); print "$line[1]";

First you split each line on white space which is almost good but given your placement of the parentheses I'm not sure you realize that you are calling split with no arguments or perhaps you think the parentheses are an argument to split.

Splitting on white space doesn't give you words, you get a list of things that could be words such as words, $var1 and "quoted?". That may or may not be what you want. I'm just saying you need to think about it.

The next two lines look like they were originally one and I'll treat them as such. Having got your list of words you then test only the second and if it contains the pattern an alphanumeric or '_', a 'P' or 'p' followed by another alphanumeric or '_' then print it. Without any surrounding white space.

What you want to do is test every word to see if has a 'P' or 'p' regardless of surrounding characters. 'Perl' will fail your test because there isn't anything to match the first \w.

You are back on track with the last line.

close ($test_fh);

Since this is homework and I'd like full marks for it I'll submit my complete worked solution

#!/net/perl/5.10.0/bin/perl use strict; use warnings; die "Invalid number of arguments\nUsage: program <file_name>\n" if @AR +GV != 1; open my $fh, '<', $ARGV[0] or die "Can't open $ARGV[0]: $!\n"; while ( my $line = <$fh> ) { print "$_\n" # print one word per line for grep {/p/i} # select those with a p or P $line =~ /(\p{IsAlpha}+)/g; # Words only have letters # and match all of them } close $fh; __END__ CPAN comprehensive Perl expression pattern pattern operator Perl help


Comment on Re: Help with split() function
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2014-10-30 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls