Kentdn has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am new perl programnming and have come up with a weee script (with the help of this site!!) to return the word in a particular position in a file. But for some reason it returns nothing. I am probably doing something wrong so if anybody can see what it is that would be fantastic. My script is as follows:
my $word1; { open( FILE, "< $filename1" ) or die( "can't find file1: $!" ); my $file = <FILE>; ( $word1 ) = $file =~ /EditorName\b.+\b(\s*)/; close FILE; } my $word2; { open( FILE, "< $filename1" ) or die( "can't find file2: $!" ); my $file = <FILE>; ( $word2 ) = $file =~ /EditorCommand\b.+\b(\s*)/; close FILE; } print $word1, $word2;
This is only a snippet but it the area of the code I am having difficulty with. Thanks for the help!

Replies are listed 'Best First'.
Re (tilly) 1: returning word from a file position
by tilly (Archbishop) on Jan 22, 2002 at 10:48 UTC
    The line:
    my $file = <FILE>;
    only reads one line from the file. You will need to loop through the file looking for your words. Secondly your pattern for a word is..interesting. I could just give you an RE that kind of works, but I suspect that you are really in need of a good basic introduction to the topic.

    Theoretically complete documentation on how REs work can be found at perlre. But that is hard for most people to follow. japhy is in the process of writing a book on how to learn basic REs. You can see the start of it here, and his tool YAPE::Regex::Explain is invaluable.

    And, of course, let me recommend merlyn's book, Learning Perl. It has an excellent review of basic Perl that should help you a lot.

Re: returning word from a file position
by Zaxo (Archbishop) on Jan 22, 2002 at 10:50 UTC

    Two problems:

    1. $file is only taking the first record from <FILE>
    2. Your regexes are capturing the last whitespace on the line, if anything.

    Here is your first block rewritten:

    my @word1 { local *FILE; # make this a 'my $fle;' in 5.6 open( FILE, "< /path/to/$filename1" ) or die( "can't find file1: $ +!" ); while (<FILE>) { push @word1, /EditorName\s+(\w+)/g; } close FILE or die $!; }
    Those blocks are so similar, I think you should consider making a sub of them. Suggested syntax findword FILENAME, PATTERN;.

    After Compline,
    Zaxo

Re: returning word from a file position
by maverick (Curate) on Jan 22, 2002 at 10:21 UTC
    change:
    ( $word1 ) = $file =~ /EditorName\b.+\b(\s*)/;
    to:
    ( $word1 ) = ($file =~ /EditorName\b(.+?)\b\s*/);
    to extract the thing matched in the ( ) the =~ has to be in array context, thus the parens. In scalar context you get the number of matches. Aside from that, I suspect that you really want the .+ part, not the \s* part. Also .+ will match as much as it can, which is probably more than you want, so the ? makes it match as little as it can...just the stuff between the two \b's.

    HTH

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

and for your future reference...
by dash2 (Hermit) on Jan 22, 2002 at 16:33 UTC
    perl -d [script name]

    will take you into the debugger, where you can run your script line by line. man perldebug will show you how to use the debugger.

    dave hj~

      Thanks for the help, really apprecaited!!!!