CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Read a file from a specific line and extract something to another file

 

First page Previous page 1 2 Next page Last page  View All


sfo_sc
Novice

Jan 19, 2005, 9:08 PM

Post #26 of 33 (2814 views)
Re: [sfo_sc] Read a file from a specific line and extract something to another file [In reply to] Can't Post

Whats wrong with this piece of code? I couldn't figure out.

Code
while($file[$i] =~ m/^\t/ || $file[$i] =~ m/^Caused/ 
|| length($file[$i]) == 0 || $file[$i] =~ m/^\s/) {
push(@temp, $file[$i] . "\n");
$i++;
}

I keep getting "Use of uninitialized value in pattern match (m//) at ./logwatch.pl line 53, <SOURCE> line 274." and go into infinit loop. Please help.

It is complaining about the leading whitespace condition I just adding. If I remove that, the code will work.

Line 53 is the $i++, which doesn't make sense at all.

Here is the case where I want to capture to the file.

Code
java.lang.InternalError: jzentry == 0, 
jzfile = 135272768,
total = 390,
name = /opt/tomcat5/webapps/fsm.war,
i = 12,
message = invalid LOC header (bad signature)
at java.util.zip.ZipFile$2.nextElement(ZipFile.java:320)

I can capture the line start with tab no problem, but I couldn't get the lines start with space.


(This post was edited by sfo_sc on Jan 19, 2005, 10:53 PM)


KevinR
Veteran


Jan 19, 2005, 10:35 PM

Post #27 of 33 (2807 views)
Re: [sfo_sc] Read a file from a specific line and extract something to another file [In reply to] Can't Post


In Reply To
Thanks for the info. I found out I can use length($string) == 0 too. =)

but when I try to look for lines that start with space and I use m/^\s/, the code will go into infinit loop.


Can you post an example of the code you are trying, because


Code
if ($string =~ /^\s/) { 
do something
}


should find lines that start with a space or a tab
-------------------------------------------------


sfo_sc
Novice

Jan 19, 2005, 10:50 PM

Post #28 of 33 (2805 views)
Re: [KevinR] Read a file from a specific line and extract something to another file [In reply to] Can't Post

Darn it! Figure out why. It is totally un-related to the regular expression. Took me whole day to figure it out. Thank you for your help KevinR


(This post was edited by sfo_sc on Jan 19, 2005, 11:21 PM)


sfo_sc
Novice

Jan 19, 2005, 11:35 PM

Post #29 of 33 (2798 views)
Re: [sfo_sc] Read a file from a specific line and extract something to another file [In reply to] Can't Post


In Reply To
Whats wrong with this piece of code? I couldn't figure out.

Code
while($file[$i] =~ m/^\t/ || $file[$i] =~ m/^Caused/ 
|| length($file[$i]) == 0 || $file[$i] =~ m/^\s/) {
push(@temp, $file[$i] . "\n");
$i++;
}

I keep getting "Use of uninitialized value in pattern match (m//) at ./logwatch.pl line 53, <SOURCE> line 274." and go into infinit loop. Please help.

It is complaining about the leading whitespace condition I just adding. If I remove that, the code will work.

Line 53 is the $i++, which doesn't make sense at all.

Here is the case where I want to capture to the file.

Code
java.lang.InternalError: jzentry == 0, 
jzfile = 135272768,
total = 390,
name = /opt/tomcat5/webapps/fsm.war,
i = 12,
message = invalid LOC header (bad signature)
at java.util.zip.ZipFile$2.nextElement(ZipFile.java:320)

I can capture the line start with tab no problem, but I couldn't get the lines start with space.


My code magically works if I add another line at the end of the read in file which doesn't match any of the case. So if my read file ends with a line that matches the case, my code will go into infinit loop. Can someone see whats wrong with my code? What is causing the problem?


sfo_sc
Novice

Jan 19, 2005, 11:38 PM

Post #30 of 33 (2797 views)
Re: [sfo_sc] Read a file from a specific line and extract something to another file [In reply to] Can't Post

I think I found the problem. Crazy
I better go get some sleep to clear up my mind.


KevinR
Veteran


Jan 20, 2005, 12:10 AM

Post #31 of 33 (2796 views)
Re: [sfo_sc] Read a file from a specific line and extract something to another file [In reply to] Can't Post

I think maybe you want to use "if" instead of "while" here:


Code
while ($file[$i] =~ m/^\t/ || $file[$i] =~ m/^Caused/  
|| length($file[$i]) == 0 || $file[$i] =~ m/^\s/) {
push(@temp, $file[$i] . "\n");
$i++;
}


"while" is generally used for looping through lists or files while a condition is true. "if" is used to make a decision based on the evaluation of an expression.

what you have is something like this:


Code
$num = 1; 
while ($num == 1) {
print "AHHHH!!";
}


since $num will always equal 1, the condition will always be true, and the loop will never exit. Now go get some sleep! Wink
-------------------------------------------------


(This post was edited by KevinR on Jan 20, 2005, 12:13 AM)


sfo_sc
Novice

Jan 20, 2005, 1:14 PM

Post #32 of 33 (2785 views)
Re: [KevinR] Read a file from a specific line and extract something to another file [In reply to] Can't Post

Something I have learn since I was studying Computer Science, once my head really gets stuck, it is usually time to go to bed! Tongue


Jean
User


Jan 23, 2005, 10:15 AM

Post #33 of 33 (2772 views)
Re: [sfo_sc] Read a file from a specific line and extract something to another file [In reply to] Can't Post

I'm not sure why the code would go into infinite loop, but this can help you:

\s matches tabs as well as other whitespace, e.g. ascii(32)


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives