Not sure whats going wrong with this one! [Lotto]

Hi

I have a list in textedit which looks like this:

john smith 3, 9, 31, 37, 40, 42
john doe 2, 5, 6, 8, 35, 38
jane smith 3, 8, 15, 35, 36, 40
bob jones 5, 7, 10, 22, 38, 40

it goes on and on for about 40 lines, there lottery numbers and the names of the people who have
picked them.
i’ve started a script which goes thru this list and checks the numbers against some numbers input into a dialog,
its working about 95% i reckon and i’m not sure where to go, i maybe need to leave it for a day and come back but thought i might post
to see if someone can point me in the right direction.
Heres my script so far

set j to {}

--set t to choose file without invisibles
set lotto_numbers to text returned of (display dialog "Enter this weeks winning lotto numbers!" & return & "Put a comma (,) inbetween each number" default answer "3,9,31,37,40,42")

tell application "TextEdit"
	launch
	--open t
	tell document 1
		set u to count every paragraph
		repeat with i from 1 to u
			--display dialog paragraph i as string
			set w to word 1 of paragraph i
			set z to word 2 of paragraph i
			my checklotto(paragraph i, lotto_numbers, j, w, z)
			set j to {}
		end repeat
	end tell
end tell

to checklotto(x, lotto_numbers, j, w, z)
	set thenumbers to words 3 thru 8 of x as list
	set astid to (AppleScript's text item delimiters)
	set (AppleScript's text item delimiters) to {", "}
	set ti_list to text items of thenumbers
	set (AppleScript's text item delimiters) to astid
	set employee_list to {}
	repeat with i in ti_list
		try
			set i to i as integer
		end try
		set end of employee_list to (contents of i)
	end repeat
	--return employee_list-- as a list of numbers
	repeat with f in employee_list
		if f is in lotto_numbers then
			copy f to end of j
		end if
	end repeat
	set s to count j
	activate me
	display dialog w & space & z & " matched " & s & space & "out of 6 numbers" as string
end checklotto

Has far as i can tell its checking the numbers quite well i’ve matched up my numbers input with the first line in the textedit doc and it gives me 6 out of 6, however when it repeats thru to the next line its giving me 1 out of 6, as far as i’m concerned theres no numbers in the second line that match so i think its picking up the 3 in the 35 or 38 although i could be wrong! (brain fizzing)
any help or guidance with this would be extremely appreciated.
In honesty its only a bit of a fun/practice script really so not massively important but if you can help thanks!!

Hi pidge,

lottonumbers is a string (your default is “3,9,31,37,40,42”)
the first number of john doe is 2 and of course the character “2” it’s in lottonumbers (42)

If would be better to coerce lottonumbers to a list

Hi Stefan

Thanks for the reply!
If i coerce to a list then everyone gives “0 matches out of 6”
i know i should be getting this!! i’m sure it should be pretty simple but i just can’t seem to see it!

thanks

try this

set j to {}

--set t to choose file without invisibles
set lotto_numbers to text returned of (display dialog "Enter this weeks winning lotto numbers!" & return & "Put a comma (,) inbetween each number" default answer "3,9,31,37,40,42")
set {TID, text item delimiters} to {text item delimiters, ","}
set lotto_numbers to text items of lotto_numbers
set text item delimiters to TID

tell application "TextEdit"
	launch
	--open t
	tell document 1
		repeat with i in paragraphs
			--display dialog paragraph i as string
			set {w, z} to words 1 thru 2 of i
			my checklotto(contents of i, lotto_numbers, j, w, z)
			set j to {}
		end repeat
	end tell
end tell

to checklotto(x, lotto_numbers, j, w, z)
	set thenumbers to words 3 thru 8 of x
	--return employee_list-- as a list of numbers
	repeat with f in thenumbers
		if {contents of f} is in lotto_numbers then copy f to end of j
	end repeat
	set s to count j
	activate me
	display dialog w & space & z & " matched " & s & space & "out of 6 numbers" as string
end checklotto

Hi, Pidge;

This might be a slightly simpler approach, where I’ve saved your data in a plain text file called Lotto.txt.


set lotto_numbers to text returned of (display dialog "Enter this weeks winning lotto numbers!" & return & "Put a comma (,) inbetween each number" default answer "3,9,31,37,40,42")

set LPicks to read alias ((path to desktop folder as text) & "Lotto.txt")
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set tNums to text items of lotto_numbers
set AppleScript's text item delimiters to tid

set P to paragraphs of LPicks
set R to ""
repeat with aP in P
	if contents of aP is not "" then
		set tP to words of aP
		set OK to {}
		repeat with k from 3 to 8
			if item (k - 2) of tNums ≠ item k of tP then set item k of tP to "**"
		end repeat
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to space
		set R to R & tP as string
		set AppleScript's text item delimiters to tid
		set R to R & return
	end if
end repeat
R

Note the (added) comma after the name.

This works for me:

choose file with prompt "Open this lottery text file:" without invisibles
set lottoText to paragraphs of (read result)

display dialog "Enter this weeks winning lotto numbers!" & return & "Put a comma (,) in between each number" default answer "3,9,31,37,40,42"
set winningNumbers to text returned of result

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set winningNumbers to text items of winningNumbers
set AppleScript's text item delimiters to ASTID

repeat with thisItem in lottoText
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ", "
	set {thisName, thisEntry} to {text item 1, text items 2 thru -1} of thisItem
	set AppleScript's text item delimiters to ASTID
	
	checkNumbers of thisEntry for winningNumbers
	
	display dialog (thisName & " matched " & result & " out of 6 numbers.")
end repeat

on checkNumbers of anEntry for theseNumbers
	set matchList to {}
	repeat with thisNumber in anEntry
		if thisNumber is in theseNumbers then set end of matchList to thisNumber
	end repeat
	return count matchList
end checkNumbers

Stefan, Adam, Bruce

Thanks fellas for your help really appreciate it!

I was lost in Applescript Spagetti!!
You’ve given me a helluva lot more to think about. i need to take it in.

Thank you!