SPUG: ifs and whiles and hashes...

David Bitseff dbitsef at uswest.com
Wed Aug 18 18:28:21 CDT 1999


If you are getting stuck in the 'if' statement the problem could be
with your regex.  A sample of the data you are matching against might
help us with your problem if the regex is the culprit.  Another option 
you may consider is to use the split function.  You could hack off the 
leading quote and then do a '@values = split(/", "/, $dbaseLine);'  


>>>>> On Wed XVIII Aug MCMXCIX XV:XXXIV:V, Ryan Forsythe writes:

Ryan> hi, it's ryan the perl newbie again, with another (probably) simple
Ryan> question...

Ryan> i'm writing a program which opens a text file, searches each line, and
Ryan> extracts data from it and sticks it in a hash...well, that's what it's
Ryan> supposed to do.  here's a simplification of the loop i'm having problems
Ryan> with:

Ryan> while (defined($dbaseLine = <DATABASE>))  {

Ryan> 	#code here...

Ryan> 	if ($dbaseLine =~ m/^\"(?:.*)\",\"(.*)\",\"(.*)\"/)  {
		
Ryan> 		$hash{'key1'} = $1;
Ryan> 		$hash{'key2'} = $2;
Ryan> 		#etc...
Ryan> 	}  else  {
Ryan> 		next;
Ryan> 	}
Ryan> }

Ryan> however, my program has 26 of these '\"(.*)\",' in the 'if
Ryan> ($dbaseLine...' test.  when i run it, it assigns the $dbaseLine variable
Ryan> okay, but when it gets to that if test, it locks up and i watch perl's
Ryan> cpu time go up to 99%.  i'm assuming it's getting in an infinite loop,
Ryan> but why?  i don't understand how an if can cause and infinite loop,
Ryan> especially when it doesn't affect the test variable of the while it's
Ryan> wrapped in (if that makes any sense to anybody :))

Ryan> btw, is there any shorter way to write that 'if' that still stores
Ryan> substrings?

Ryan> --ryan

Ryan>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ryan>     POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
Ryan>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
Ryan>  SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
Ryan>         Email to majordomo at pm.org: ACTION spug-list your_address


-- 
David Bitseff
U S WEST Creative Services
dbitsef at uswest.com         
(206) 346-9279

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list