|Perl: the Markov chain saw|
Re: Help with split() functionby hipowls (Curate)
|on Mar 19, 2008 at 10:19 UTC||Need Help??|
As others have pointed out your code is very wrong in places. I'll go over it line by line
so far so good
Now it starts to go awry
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
OK although many people would explicitly assign <test_fh> to a lexical variable.
Now we get to the real problems
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.
Since this is homework and I'd like full marks for it I'll submit my complete worked solution