SPUG: ifs and whiles and hashes...

Scott Blachowicz Scott.Blachowicz at seaslug.org
Wed Aug 18 18:55:27 CDT 1999


> > 	if ($dbaseLine =~ m/^\"(?:.*)\",\"(.*)\",\"(.*)\"/)  {
> ...
> I think you can also do something  like:
> 	(the_match){26}
> to say you want 26 of those matches, but I wasn't getting 26 variables
> populated in my test case, just $1 holding the last match.  There might
> be a way to do this, but off the top of my head (here on my reclining
> chair in full sunlight, without recourse to Friedl's book) this is as
> close as I can come right now! 8-}

Depends...are there commas in the $dbaseLine that are NOT delimiters?
Another way to approach it might be something like:

	@tokens = split(/,/, $dbaseLine);
	if (@tokens == 26) {
	   # Individual tokens are in $tokens[0] thru $tokens[25] - just
	   # need to strip leading & trailing quotes.
	}

Also...can you have embedded quotes inside your individual tokens?  If
so, that regexp will need some tweaking...maybe something like:

	if ($dbaseLine =~ m/^\"(([^"]|\\\")*)",/) {

to match one of them (getting rid of the ".*" construct probably means
you can get rid of the "non-greedy" "?:" as well.

And much as I hate to do explicit loops...maybe...

	my @tokens;
	my $token;
	my $arg2;
	my $rest = $dbaseLine;
	while (($token,$arg2,$rest) = ($rest =~ m/^\"(([^"]|\\\")*)"(,.*|$)/)) {
	  push (@tokens, $rest);
	}
	if (@tokens == 26) {
	}

(NOTE: The perl code quoted here is completely free from having been
       run, tested or otherwise validated...)
--
Scott.Blachowicz at seaslug.org

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    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