in reply to read a word from a line

When you read in a line from a file like this while(<PAGE>), $_ stores the whole line you just read in. So print "$_" prints the whole line.

To print out only part of the line, you need to use a capturing regular expression. You will also need to escape the ( and ) because those have special meaning in a regular expression. So to get the stuff between the parenthesis after pond:

print $1 if (/pond\(([^)]*)\)/);

[^)]* means match everything up until the first closing ). ([^)]*) says to capture it and store it in $1. \( and \) say "treat ( and ) as normal parenthesis". See perlretut for more information.

Best, beth

Update:Fixed typo, as per AnomalousMonk below. Thanks.

Replies are listed 'Best First'.
Re^2: read a word from a line
by AnomalousMonk (Archbishop) on Feb 25, 2009 at 15:31 UTC
    [^)*] means match everything up until the first closing ).
    [^)*] means "match a single character that is anything other than a ')' or a '*'".

    [^)]* means "greedily match zero or more of any character that is not a ')'".

Re^2: read a word from a line
by sharan (Acolyte) on Feb 25, 2009 at 17:11 UTC
    Thanks for ur reply.. I tried with ur command.. but its not working as wat i expect. My intention is just to read the value between the brackets.. i.e. between "(" and ")". Thats why my desired output is end and start. Thanking you,
      Perhaps you tried the regular expression before it was corrected? The original version had a typo and I apologize for that. The code above (corrected as per AnomalousMonk) does indeed extract just what is between the parenthesis. However, it can't be used just by itself. It has to be inserted into a loop that reads in each line. A lot depends on exactly how you do it.

      If you are having trouble getting it to work, perhaps you should consider adding an update to your original question and post the code where you are using the regular expression (or just post it in reply to this node). Then we might be able to see if there are other issues besides the choice of regular expression that are causing problems.

      Best, beth