Page 1 of 1
delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 3:42 pm
by jon
Hi
I am putting names and scores into a fld,
so if the cHiScoreName is already in the fld I need to delete the line that contains the same name in the fld before I update with new data
I am having problems working out how to delete the line.
Code: Select all
set the cHiScore of fld "alltimehighScorefld" to fld "alltimehighScorefld"
set the cHiScoreName of fld "alltimehighScoreName" to fld "alltimehighScoreName"
put the cHiScoreName of fld "alltimehighScoreName" after fld "showHiScores"
put space& the cHiScore of fld "alltimehighScorefld" &cr after fld "showHiScores"
My fld contains something like this
Jon 23456
Fred 34345
Sue 12875
Any advice is appreciated, Thanks, Jon
Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 5:49 pm
by mwieder
Jon-
Probably any number of ways to deal with this, but for this sort of situation I like to use an array:
Code: Select all
-- keep track of each player's score in an array
put 23456 into tScoresArray["Jon]
put 34345 into tScoresArray["Fred"]
put 12875 into tScoresArray["Sue"]
--then when you're ready to display the scores
combine tScoresArray using cr and space
sort tScoresArray descending by item 2 of each
put tScoresArray into field "showHiScores"
Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 5:58 pm
by dunbarx
Hi.
What Mark said.
But if this is a little too far advanced for you, think about ordering all your lines by sorting them. Then do this:
put fld "yourScoreField" into temp
repeat with y = the number of lines of temp down to 1
if word 1 of line y of temp = word 1 of line (y -1) of temp then delete line y of temp
end repeat
answer temp
See? The loop compares the first word of each line with the first word of the line above it, and if they match, it deletes that line. Now why are we doing this from the bottom up instead of from the top down?
Craig Newman
Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 6:04 pm
by Klaus
Hi Jon,
to replace a line for a player you can use "lineoffset" to find the "old" hiscore of that player like this:
Code: Select all
## We take this for lineoffset -> playername & SPACE
put the cHiScoreName of fld "alltimehighScoreName" & SPACE into tPlayer
## Create new entry
put tPlayer & the cHiScore of fld "alltimehighScorefld" into tNewHiScoreLine
## Put field content into variable, always faster than using the field itself!
put fld "showHiScores" into tList
## Now we check if this player already has an entry, will of course only work fine with unique names!
put lineoffset(tPlayer,tList) into tOldLine
## tOldLine will now contain 0 if the player does not have an entry yet or the line NUMBER of the old entry in the list, so we can simply replace that line!
if tOldLine = 0 then
## We append that new hiscore in this case
put CR & tNewHiScoreLine AFTER tList
else
## We simplyreplace the old line
put tNewHiScoreLine INTO line tOldLine of tList
end if
put tList into fld "showHiScores"
Best
Klaus
Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 6:06 pm
by Klaus
dunbarx wrote:Now why are we doing this from the bottom up instead of from the top down?
Because it's tuesday?
OK, Jon, pick one

Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 9:14 pm
by jon
Love the sense of humour guys, I will play about with all the posted code simply to learn more.
Thanks, as always to all of you, one day in my next life I may be able to work it out myself.
And because the highest score is needed to be kept?
Jon

Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 9:27 pm
by mwieder
Craig's remark about doing the loop from the bottom up is quite on point here, and is intended as a learning moment. A little reflection on the loop will show why it's necessary.
Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 11:24 pm
by jon
Is it so the highest score with that name will not be deleted?
Re: delete a duplicate line of a fld
Posted: Tue Sep 04, 2012 11:43 pm
by mwieder
Nope. The content of the lines isn't important for this.
If you step through the lines of code with a debugger it will start to reveal why.
Hint: if you have ten lines of scores, what happens after you remove line 3 and you're still stepping through numbered lines?
Re: delete a duplicate line of a fld
Posted: Wed Sep 05, 2012 1:16 am
by dunbarx
Hi.
Did you want the highest score to be retained? I did not know this. You need to :
sort lines of temp by word 1 of each
sort lines of temp descending numeric by word 1 of each & word 2 of each
Craig Newman
Re: delete a duplicate line of a fld
Posted: Wed Sep 05, 2012 8:11 am
by jon
Craig
if word 1 of line y of temp = word 1 of line (y -1) of temp then delete line y of temp
Because it would delete itself if it went top down? or maybe because it's now wednesday?
put fld "showHiScores" of stack Scores into temp
repeat with y = the number of lines of temp down to 1
if word 1 of line y of temp = word 1 of line (y -1) of temp then delete line y of temp
end repeat
answer temp
The above code only removes the duplicate line if it is immedietly above
example :-
Jon 1234
sue 3456
fred 3456
sue 5678
it stops at fred so does not delete "sue" above fred, why is that?
sort lines of temp by word 1 of each
sort lines of temp descending numeric by word 1 of each & word 2 of each
And the sorting for highest I really did work out for myself

I must be doing something right.
Jon
Re: delete a duplicate line of a fld
Posted: Wed Sep 05, 2012 8:24 am
by jon
Hmmm
Just a thought, I want to eventually allow users to post hi Scores to a web page, so may not be a good idea to delete a players score that has the same name
because any player could then delete a players score simply because they have the same name but a lower score, I think I should include something that compares
same names and then compares word 2 of y and keep the higher scoring player with that name, these are my thoughts, does that sound feasable? or is there a better way?
Klaus,
to replace a line for a player you can use "lineoffset" to find the "old" hiscore of that player like this:
That works great for me, I am now trying to get my head around WHY it works great, I will study it to find out why it only works on tuesdays though
Thanks, Jon
Re: delete a duplicate line of a fld
Posted: Wed Sep 05, 2012 11:43 am
by Klaus
Hi Jon,
jon wrote:Klaus,
to replace a line for a player you can use "lineoffset" to find the "old" hiscore of that player like this:
That works great for me, I am now trying to get my head around WHY it works great,...
The explanation is in my script!
jon wrote:... I will study it to find out why it only works on tuesdays though

Hmmm, THAT was another context, "lineoffset" will work 24/7!
Best
Klaus
Re: delete a duplicate line of a fld
Posted: Wed Sep 05, 2012 5:01 pm
by jon
Hi Klaus
Yes I can see how it works when I go through your //explanations
One by one.
I'll try it on Tuesday.
Many thanks to all, Jon
Re: delete a duplicate line of a fld
Posted: Wed Sep 05, 2012 10:16 pm
by mwieder
I'll try it on Tuesday.
Actually, it only works on days that end in "y"
(...and for Klaus, days that end in "g" or "h") + (and sometimes "d", location-dependent)