[prog] Re: [Courses] String Comparation...

Jacinta Richardson jarich at perltraining.com.au
Fri Oct 4 15:02:22 EST 2002


> Situation:
> I have *one* line data stored in hostname,
> I also have *many-many* lines of data stored in
> buffer....
> 
> Question:
> How to compare hostname and buffer (line by line), to
> check is there a match between the *one* line in
> hostname with *any line* in buffer....

Right.... What language?  I'm sorry, this is probably to do with a course
I wasn't paying attention to.  I'll pretend you mean C for a moment:

char line[80];		/* one line of data */
char buffer[100][80]	/* 100 lines of data */
int i;

for(i = 0; i < 100; i++)
{
 	if(strstr(buffer[i], line))
	{
		printf("we found it (element %i)!\n", i);
	}
}


That MIGHT work in C.  I haven't tested it.  I haven't written C code for
more than 2 years... (in recovery ;) )

Perl on the other hand:

my $line	= "search for this";		# a single line

my @buffer 	= ("search high and low",	# lots of lines
                   "search near and far",
                   "search long and hard",
                   "to search for this");
foreach my $poss (@buffer)
{
	if( $poss =~ /$line/ )
	{
		print "we found it!\n";
	}
}

	# or, if you care about what line it was on:
for(my $i = 0; $i < @buffer; $i++)
{
	if( $poss =~ /$line/ )
	{
		print "we found it! (line $i)\n";
	}
}



If these don't help, perhaps you'll show us how you've been trying to
answer the question and we can guide you from there.

	Jacinta

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |





More information about the Programming mailing list