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


In reply to Re: Help with split() function by hipowls
in thread Help with split() function by negzero7

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.