Re: A question on technique
Re: A question on technique
- Subject: Re: A question on technique
- From: "Stockly, Ed" <email@hidden>
- Date: Mon, 16 Mar 2015 14:53:58 +0000
- Thread-topic: A question on technique
You’ve gotten some good answers here, so this is a bit late, but here are two different approaches.
The first is pure simple appleScript using lists, which should be fine for short lists (dozens, or low hundreds)
The second uses text coercions and would be much faster with long lists (hundreds, thousands or more)
They both assume all values are text and they can be any length. The text version assumes none of the values have returns or tabs.
Both handlers should generate identical results.
property Codes : {"9420", "94ae", "94d0", "942f", "942C", "9470"}
property Mnemonics : {"RCL", "ENM", "ReW", "EOC", "EDM", "RfW"}
set cAndMPairs to {}
set cAndMTextPairs to {""}
set AppleScript's text item delimiters to {tab} --must be something not in the codes list
repeat with x from 1 to count of Codes
set the end of cAndMPairs to {item x of Codes, item x of Mnemonics}
set the end of cAndMTextPairs to {item x of Codes as text, item x of Mnemonics as text} as text
end repeat
set AppleScript's text item delimiters to {return}
set cAndMTextPairs to cAndMTextPairs as text
log cAndMPairs
log cAndMTextPairs
set mnemonicList to {}
set mnemonicTextList to {}
repeat 100 times
set aCode to some item of Codes --picks a random item
if (random number from 1 to x) is in {2} then set aCode to "errr" --generates not found, remove before going live!
set the end of mnemonicList to MnemonicOf(aCode as item, cAndMPairs)
set the end of mnemonicTextList to MnemonicOfText(aCode as item, cAndMTextPairs)
end repeat
log mnemonicList
log mnemonicTextList
on MnemonicOf(aCode, cAndMPairs)
repeat with thisPair in cAndMPairs
set {thisCode, thisMnemonic} to thisPair as item
if aCode = thisCode then return thisMnemonic
end repeat
return false
end MnemonicOf
---or
on MnemonicOfText(aCode, pairText)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return & aCode & tab}
try
set thisMnemonic to word 1 of text item 2 of pairText
on error
set thisMnemonic to false
end try
set AppleScript's text item delimiters to saveTID
return thisMnemonic
end MnemonicOfText
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden