[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] sub-search match novice question



On Sat, 4 Sep 1999 22:51:28 -0400, Randy M. Zeitman wrote:

>I'm matching a string to text file of records (rows). Each entry (column)
>in each record is tab separated (a la spreadsheet).
>
>But sometimes I want to only match against columns 2,3,and 4, of all
>records and other times I want to match only against column 7. Any quicker
>way to do this than to parse each record into an array toss the unwanted
>entries and re-string the thing? (a guess...)

You could.

>Don't actually need the answer, but a kick in the right direction...how
>might I do this with one elegant match?


	local $" = "\t"; # why not? 
	while(<FILE>) {
		chomp;
		my @field = split /\t/;
		if("@field[2,4,7]" =~ /searchterm/) {
			print "Got a match in $_\n";
			# prints record
		}
	}


Alternatively, search each of the columns in a loop. This may or may not
be faster, as restringifying is a bit of an expensive task. Could be.
Try a benchmark, if speed really bothers you.

	
		foreach (@field[2,4,7]) {
			/searchterm/ and 
				print "Got a match in $_\n";
			#prints field, not whole record
		}


And if you don't know beforehand WHICH columns you want searched, you
can do something like this:

	@columns = (2, 4, 7);
	if("@field[@columns]" =~ /searchterm/) { ... }
	foreach(@field[@columns]) { ... }

-- 
	Bart.

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org