Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assign my variables??

Status
Not open for further replies.

lawsoncs

Technical User
Oct 3, 2001
16
US
Hello, I have a small file produced that reads teh following:

Score: 192, lines:30 'display.doc /home/kgo11/docroot/'

and it has multiple lines liek this. I have set up my perl script to read teh file line by lise using

while ( $line = <FILE> )
{
($Score, $Lines, $File, $Dir) = /Score: (\d+), lines:(\d+) '(\w+) * (\w+)'.

Can you see what the problem is and wy it is missing all of the variables
 
The problem is that '\w' will not match '.' in 'display.doc' or '/' in '/home/kgo11/docroot/'. '\w' is equivalent to [a-zA-Z0-9_]. Try changing the '\w's to '\S's, the non-whitespace character.

Also, the while-loop should either be
Code:
while (<FILE>) {
     # $_ contains the current line
     (...) = /.../;
     # /../ matches against $_ by default
}
Or
Code:
while ( defined($line=<FILE>) ) {
     # $line contains the current line
     (...) = $line =~ /.../;
     # Specify to match against $line;
}

(I assume the missing '/' and ';' from the end of your line is just a typo.)

jaa
 
Or you could do the following to parse out the information....

while(<FILE>) {
chomp();
($score,$lines,$file,$dir) = /Score:\s(\d+),\slines:(\d+)\s'(.*)\s+(.*)'/;
}

Hope this helps!
 
Non-greedy matching should be used here. If there is more than one space between the filename and the path (which it looks like there is) then [tt]$file[/tt] will contain trailing whitespace because the first (.*) will gobble it all up except for the very last space before the path.
[tt]
($score,$lines,$file,$dir) = /Score:\s(\d+),\slines:(\d+)\s'(.*?)\s+(.*)'/;
[/tt]

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top