Page 1 of 1
how to replace item in a variable
Posted: Fri Feb 21, 2014 5:29 am
by keram
Hello,
This is probably a VERY basic question but I just cannot get it working:
I have 2 fields: a and b
field a contains few tab delimited numbers: 002 005 009 and I put it into variable: numbers
field b contains tab delimited rows and columns and I put it into variable: falsetrue
001 false
002 false
003 false
004 false
005 false
006 false
007 false
008 false
009 false
010 false
I want to replace false with true in falsetrue in the lines that have the same numbers as in numbers
using this code:
Code: Select all
local Counter
on mouseUp
set itemDel to tab
put fld "a" into numbers
put fld "b" into falsetrue
repeat for each line i in falsetrue
if item 1 of i is among the items in numbers then
put "true" into item 2 of i
add 1 to Counter
end if
end repeat
put falsetrue into msg box
end mouseUp
Please let me know, where is the fault?
Thanks.
keram
Re: how to replace item in a variable
Posted: Fri Feb 21, 2014 6:26 am
by dunbarx
Well, first off, you must use the keyword "of" instead of "in" when using the "is among" operator. Though these two may often be interchanged, they cannot be here.
Anyway, try this:
Code: Select all
on mouseUp
set itemDel to tab
put fld "a" into numbers
put fld "b" into falsetrue
repeat for each line i in falsetrue
if item 1 of i is in numbers then
put "true" into item 2 of i
add 1 to Counter
end if
end repeat
put falsetrue into msg box
end mouseUp
What does "counter" do?
I have not really gone into this deeply, but the problem was in the "is among the items..." line. I just cut through that with a broader brush. Go back, though, and see if you can figure out where it failed. Write back when you do.
Craig Newman
Re: how to replace item in a variable
Posted: Fri Feb 21, 2014 9:36 am
by keram
Thanks Craig,
It works now with a small addition:
put i & cr after temp
so the code is:
Code: Select all
on mouseUp
set itemDel to tab
put fld "a" into numbers
put fld "b" into falsetrue
answer falsetrue
repeat for each line i in falsetrue
if item 1 of i is in numbers then
-- if item 1 of i is among items of numbers then -- this works equally well
put "true" into item 2 of i
end if
put i & cr after temp
end repeat
put temp into falsetrue
put falsetrue into msg box
end mouseUp
dunbarx wrote:What does "counter" do?
It's not needed here in this form of the repeat.
So now I have 2 questions that follow this:
1. Suppose that I have 1000 lines in field b and 300 numbers in field a. Is this method, of repeating/looping through each line the fastest one?
What could be other ways?
2. If I have now the new field b with these rows and columns that was put into variable falsetrue:
001 false
002 true
003 false
004 false
005 true
006 false
007 false
008 false
009 true
010 false
and a 3rd field c like this that was put into variable text:
001 false gasfgagag
002 false gsdgjdj
003 false xbxbxvb
004 false xcvbsdhs
005 false ghfjfgj
006 false shhsh
007 false fgjhfhjfgj
008 false sfgsgs
009 false dghsaf
010 false wytufdhj
What is the fastest way of replacing false with true in the lines of text variable that have the same numbers as true numbers in falsetrue?
Of course I could extract the numbers from falsetrue lines that have true and replace false with true in text variable in the same way as above.
But is there some faster, direct way, without that intermediary step?
Maybe merging falsetrue with text in some way and then deleting one of the number columns and the complete false column?
Or any other way?
keram
Re: how to replace item in a variable
Posted: Fri Feb 21, 2014 10:51 am
by keram
OK, I tried with the following:
(had to change the variable text to textlines)
Code: Select all
on mouseUp
local falsetrue,textlines
set itemDel to tab
put fld "b" into falsetrue
put fld "c" into textlines
repeat with i=1 to number of lines in textlines
if item 2 of line i of falsetrue is "true" then
put "true" into item 2 of line i of textlines
end if
put line i of textlines & cr after temp
add 1 to i
end repeat
put temp into textlines
put textlines into msg box
end mouseUp
but it puts this into msg box:
001 false gasfgagag
003 false xbxbxvb
005 true ghfjfgj
007 false fgjhfhjfgj
009 true dghsaf
What's wrong?
keram
Re: how to replace item in a variable
Posted: Fri Feb 21, 2014 3:11 pm
by dunbarx
Keram.
What happens when you step through the handler? You are going to find something, perhaps the counter is not correlating the correct relationship between the number of lines in textlines to the number of lines of falseTrue?
Where is the odd garbled text coming from? It has to exist somewhere in your fields, no?
Craig
Re: how to replace item in a variable
Posted: Fri Feb 21, 2014 5:41 pm
by keram
Hi Craig,
dunbarx wrote:
What happens when you step through the handler?
I've never done it before but I assume it means setting Breakpoints at certain lines.
I did at lines 7 - 13 which is the repeat control structure, but it loops just 6 times and then puts the result in the msg box. It should loop 10times right?
dunbarx wrote:Where is the odd garbled text coming from? It has to exist somewhere in your fields, no?
I just typed it in quickly just to have some text in these fields. Yes it exists in field c and variable textlines.
Both fields have the same number of lines...
Still don't know where to correct it.
keram
Re: how to replace item in a variable
Posted: Fri Feb 21, 2014 6:54 pm
by dunbarx
Hi.
Just know that you can place a temporary breakpoint by simply clicking in the line number column. Or you can place a "breakpoint" command in the code itself. The breakpoint should go at the "if" line
If your loop only runs six times, it means that the loop index, loaded via the property you specified (the number of lines in textLines) was initialized at 6, the number of lines in that variable.
Anyway, is the "put line i of textLines..." properly placed? It seems it ought to go inside the "if" structure. Watch absolutely everything when you step through.
The good news is that you will soon figure this out.
Craig
Re: how to replace item in a variable
Posted: Fri Feb 21, 2014 7:52 pm
by jacque
In your handler, you're using a counting variable "i", which increments by one at the top of each iteration. Within the repeat loop you also add 1 to i. That increments it again, which is why it is skipping every other line. Delete the "add 1 to i" line and it should work.
This handler will fail if the two lists do not have exactly the same number of lines, which your first post indicated was the case. I'd loop through the smaller set rather than the larger, main set. It will not only be faster, but you'll avoid trying to find nonexistent lines.
You'll get even faster results if you split the sets into arrays with the initial numerical item as keys.
Code: Select all
on mouseUp
local falsetrue,textlines
put fld "b" into falsetrue
split falsetrue by cr and tab
put fld "c" into textlines
split textlines by cr and tab
set the itemDel to tab
repeat for each line L in keys(falsetrue)
if item 1 of falsetrue[L] is "true" then
put "true" into item 1 of textlines[L]
end if
end repeat
combine textlines by cr and tab
put textlines into msg box
end mouseUp
Re: how to replace item in a variable
Posted: Sat Feb 22, 2014 3:31 am
by keram
Hi Craig,
Thanks for your tips and clarifying how breakpoints work.
Yes, I had to review how counters and repeat structures work.
dunbarx wrote:is the "put line i of textLines..." properly placed?
Yes, it is, since I want to get all the lines - not only the ones with true.
Cheers.
keram
Re: how to replace item in a variable
Posted: Sat Feb 22, 2014 4:01 am
by keram
Hi Jacque,
Thanks a lot for your comments, correcting the mistakes and tips
jacque wrote:This handler will fail if the two lists do not have exactly the same number of lines, which your first post indicated was the case.
Do you mean the very first one at the top? The 2 lists: one with just 3 numbers and the second with 10 lines?
If yes,
jacque wrote:I'd loop through the smaller set rather than the larger, main set.
Then if I loop through the list with these 3 numbers I can only get the result list with only the true ones, but I'd like to get all 10.
But if you mean these ones:
001 false
002 true
003 false
004 false
005 true
006 false
007 false
008 false
009 true
010 false
and
001 false gasfgagag
002 false gsdgjdj
003 false xbxbxvb
004 false xcvbsdhs
005 false ghfjfgj
006 false shhsh
007 false fgjhfhjfgj
008 false sfgsgs
009 false dghsaf
010 false wytufdhj
then they both have the same number of lines...
keram
Re: how to replace item in a variable
Posted: Sat Feb 22, 2014 7:07 am
by jacque
You can loop through the shorter set and still get all the lines. Just replace what's in the longer list with the values in the shorter one:
Code: Select all
on mouseUp -- loop through shorter list
local falsetrue,textlines
set itemDel to tab
put fld "b" into falsetrue
put fld "c" into textlines
repeat with i=1 to number of lines in falsetrue
if item 2 of line i of falsetrue is "true" then
put "true" into item 2 of line (word 1 of line i of falsetrue) of textlines
end if
end repeat
put textlines into msg box
end mouseUp
The array method is much faster, but this one may be easier to understand.
Re: how to replace item in a variable
Posted: Sat Feb 22, 2014 11:18 am
by keram
Thanks again Jacque,
It's clear now. It's good to learn from examples.
Array method is also quite understandable, I may use it for lager lists.
keram