Searching a list of values query...

Hi all,

Just a quick query regarding searching a list of values.

Supposing I have the list:-

Is there a way of searching the list for ‘XYZ123’ and finding the item with only ‘XYZ123’ and not the ones with the a,b,c after the ‘XYZ123’ ?

Thanks in advance.


set testlist to {"", "", "", ""}
set found to {}

repeat with anitem in testlist
	set AppleScript's text item delimiters to "."
	if text item 1 of anitem ends with "XYZ123" then set end of found to contents of anitem
	set AppleScript's text item delimiters to ""
end repeat

--> {""}


Thanks for the help Alastor :slight_smile:

I’d thought of splitting things down with delims, to give me the middle bit, but wasn’t sure where I go from there. Need to go back and have another read of my AS books to remind me of a few things! Your solution is short and simple, I’ve learnt something new.

Thanks once again for the help.

Hey TecNik,

Here’s one more method using the text instead of a list.

set _text to "
set shCMD to "<<< " & quoted form of _text & " sed -En '/XYZ123\\.xxx/p'"
set foundText to do shell script shCMD

Now, let’s rewrite Alastor’s script a little with something I just learned on another thread.

Let’s also move the TIDs settings outside of the loop.

set testlist to {"", "", "", ""}

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
repeat with anitem in testlist
	if text item 1 of anitem does not end with "XYZ123" then set contents of anitem to 0
end repeat
set AppleScript's text item delimiters to oldTIDS
set testlist to text of testlist

This method is great if you don’t need to reuse your original list.

Another version of ccstone’s example where this example is more strict:

set bashList to ""

paragraphs of (do shell script "egrep '^[A-Z]{3}_XYZ123\\.xxx$' <<<" & quoted form of bashList)


Another variation, of another of ccstone’s scripts, that may be a fraction faster.

The idea is that we know in those cases that the criteria should end with a dot, so we use the dot in the criteria, which is used as text item delimters, that way, we can just count the matches, which should be faster at this stage than looking for a match. I have also used a script object to speed up the fetching of the items. We also spare the original text.

set _text to "

to partialMatch(criteria, theText)
	script o
		property l : missing value
	end script
	set o's l to paragraphs of theText
	set {tids, text item delimiters} to {text item delimiters, criteria}
	repeat with i from 1 to count o's l
		if (count text items of item i of o's l) < 2 then
			set item i of o's l to 0
		end if
	end repeat
	set text item delimiters to tids
	return o's l's text
end partialMatch

log partialMatch("XYZ123.", _text)


Removed a small bug, that was not taking height for more than one partial match in one paragraph.

Yo! That get’s up and gallops. :cool:

Faster yet is the Satimage.osax:

set _text to "
set _list to find text "^.*XYZ123\\..*" in _text with regexp, all occurrences and string result