Searching a list of values query...

Hi all,

Just a quick query regarding searching a list of values.

Supposing I have the list:-

ABC_XYZ123.xxx
EFG_XYZ123a.xxx
HNJ_XYZ123b.xxx
KLM_XYZ123c.xxx

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.

Yes.

set testlist to {"ABC_XYZ123.xxx", "EFG_XYZ123a.xxx", "HNJ_XYZ123b.xxx", "KLM_XYZ123c.xxx"}
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

found
--> {"ABC_XYZ123.xxx"}

:cool:

Brilliant!
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 "
ABC_XYZ123.xxx
EFG_XYZ123a.xxx
HNJ_XYZ123b.xxx
KLM_XYZ123c.xxx
"
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 {"ABC_XYZ123.xxx", "EFG_XYZ123a.xxx", "HNJ_XYZ123b.xxx", "KLM_XYZ123c.xxx"}

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 "ABC_XYZ123.xxx
AABC_XYZ123.xxx
EFG_XYZ123a.xxx
HNJ_XYZ123b.xxx
KLM_XYZ123c.xxx"

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

Hello.

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 "
ABC_XYZ123.xxx
EFG_XYZ123a.xxx
HNJ_XYZ123b.xxx
KLM_XYZ123c.xxx
"


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)

Edit

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 "
ABC_XYZ123.xxx
EFG_XYZ123a.xxx
HNJ_XYZ123b.xxx
KLM_XYZ123c.xxx
"
set _list to find text "^.*XYZ123\\..*" in _text with regexp, all occurrences and string result