How to return the nth occurrence of a word?

Forum closed. All further discussion to be discussed at https://github.com/OpenKore/

Moderator: Moderators

Message
Author
unsafe1413
Noob
Noob
Posts: 3
Joined: 15 May 2011, 06:05
Noob?: No

How to return the nth occurrence of a word?

#1 Post by unsafe1413 »

Help me to return the nth occurrence of a word,

Example:

Code: Select all

--------------0-------1---------2-----------3
--------------1-------2---------3-----------4
$itemlist = apple, banana, red potion, white potion
I will search red potion
The result must be 2(0 starting count) or 3

Code: Select all

automacro itemsearch{
	console /Find item (.*)/
	call {
        $itemlist = apple, banana, red potion, white potion
	if (existsInList("$itemlist", "$.lastMatch1")) goto found
	do c Item not found!
        stop

	:found
	do c Item found!
	}
}

sub existsInList {
	my ($list, $val) = @_;
	return 0 if ($val eq "");
	my @array = split / *, */, $list;
	$val = lc($val);
	foreach (@array) {
	s/^\s+//;
	s/\s+$//;
	s/\s+/ /g;
	next if $_ eq "";
	return 1 if lc eq $val;
	}
	return 0;
}

Locked