Repat on variable

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
brandcode
Posts: 28
Joined: Mon Dec 01, 2014 12:28 pm

Repat on variable

Post by brandcode » Sat Mar 28, 2015 2:28 pm

I have a number of fields in my group and i have a variable.
I want to rename each field inside the group with the names from variable.
but with the bellow code i can make it to work

Code: Select all

 put the keys of tArray[response] into tKeys
     repeat with x=1 to the number of fields of group "grp1"
          add 1 to counter
          put line 1 of tkeys into f
          delete line 1 in tKeys
          if the disabled of fld x of group "grp1"is true then
               set the name of fld x of group "grp1" to f
               set the text of fld x of group "grp1" to f
          end if
     end repeat     
end mouseUp
my array looks like this
Array[response]
and the keys
Last edited by brandcode on Sat Mar 28, 2015 6:16 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Repat on variable

Post by dunbarx » Sat Mar 28, 2015 3:52 pm

Hi.

Why an array? Anyway, I did not test your code, but what is different between your approach and the following button script. I made a group of fields on a new card.

Code: Select all

on mouseUp
   put "newName" into f
   repeat with y = 1 to the number of flds of grp 1
      set the name of fld y of grp 1 to f & y
   end repeat
end mouseUp
Just an aside, it is very important to reference the group in the "set" line. Do you know why?

Craig Newman

brandcode
Posts: 28
Joined: Mon Dec 01, 2014 12:28 pm

Re: Repat on variable

Post by brandcode » Sat Mar 28, 2015 4:04 pm

dunbarx wrote:Hi.

Why an array? Anyway, I did not test your code, but what is different between your approach and the following button script. I made a group of fields on a new card.

Code: Select all

on mouseUp
   put "newName" into f
   repeat with y = 1 to the number of flds of grp 1
      set the name of fld y of grp 1 to f & y
   end repeat
end mouseUp
Just an aside, it is very important to reference the group in the "set" line. Do you know why?

Craig Newman
hi Craig thank you for the fast reply.

Array because is coming from json parse, and the key names is random.
Group with fields because the number of fields is random ,maybe 5 fields or 20 depend from json parse.
So when i do the repeat in array that line will be the name of the field.
Just an aside, it is very important to reference the group in the "set" line. Do you know why?
yes because if i don't set will be change the fields outside of the group ?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Repat on variable

Post by dunbarx » Sat Mar 28, 2015 4:23 pm

Right on.

But does your handler work? Are you asking for help or just posting for comments?

Craig

brandcode
Posts: 28
Joined: Mon Dec 01, 2014 12:28 pm

Re: Repat on variable

Post by brandcode » Sat Mar 28, 2015 4:33 pm

dunbarx wrote:Right on.

But does your handler work? Are you asking for help or just posting for comments?

Craig
until now is no working.
in the line 3 ( put tLine into f) i get always the same name like "test1"
And with the delete in line 5 still can read the next line in my array.
i think because is the repeat inside the repeat?
I am a confuse with the repeat command.i am sure is something wrong with my code..
I want to get each line from array A and rename each name in the fields with array name.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Repat on variable

Post by SparkOut » Sat Mar 28, 2015 6:08 pm

What is the purpose of the line "delete line 1 in my keys"?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Repat on variable

Post by dunbarx » Sat Mar 28, 2015 6:11 pm

It sometimes is intrinsically bad to delete lines of a working dataSet starting from the top, following some process. It is just the way we count the integers. This usually pops up when an indexed repeat is running, (repeat with...).

Is it possible that something similar is happening here as well, as Sparkout alluded to? If you step through the handler, does the number of lines and their order keep to your intent?

Craig

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Repat on variable

Post by SparkOut » Sat Mar 28, 2015 6:46 pm

Why did you edit the code in the original post?
I think you had it nearly right in the first place there, but when deleting the first line of the keys in the loop you threw it out.
I think. I am still not quite sure what you were expecting, although your edit makes me think you were originally on the right lines a little more. But can you explain which field names should be changed? Only change the corresponding field number with the corresponding key line number? (It is very dangerous to rely on the order of an array's keys without specifically sorting them.)

brandcode
Posts: 28
Joined: Mon Dec 01, 2014 12:28 pm

Re: Repat on variable

Post by brandcode » Sat Mar 28, 2015 6:59 pm

SparkOut wrote:Why did you edit the code in the original post?
I think you had it nearly right in the first place there, but when deleting the first line of the keys in the loop you threw it out.
I think. I am still not quite sure what you were expecting, although your edit makes me think you were originally on the right lines a little more. But can you explain which field names should be changed? Only change the corresponding field number with the corresponding key line number?
my array have 10 keys like bellow,and i want only the key name

Code: Select all

tAarray
NameA and the value
and in the group with fields i have 10 fields with No Name on them (label 1,etc)
so the result i want, is to get the key name from the array, and rename all the field (names)

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Repat on variable

Post by SparkOut » Sat Mar 28, 2015 7:34 pm

But do you want all the field names to change to the same thing?
Presumably you have an array such as:
tArray["NameOne"] with value "Data1"
tArray["NameTwo"] with value "Data2" etc. etc.

So guessing you want field 1 of the group to have its name set to "NameOne" and field 2 of the group to have the name set to "NameTwo" and so on, is that right?

Because unless you are able to SORT the keys of the array to a corresponding order for the fields in the group to match the field number, you will have a problem, as the keys for an array may be retrieved in any arbitrary order.

brandcode
Posts: 28
Joined: Mon Dec 01, 2014 12:28 pm

Re: Repat on variable

Post by brandcode » Sat Mar 28, 2015 7:37 pm

SparkOut wrote:But do you want all the field names to change to the same thing?
Presumably you have an array such as:
tArray["NameOne"] with value "Data1"
tArray["NameTwo"] with value "Data2" etc. etc.

So guessing you want field 1 of the group to have its name set to "NameOne" and field 2 of the group to have the name set to "NameTwo" and so on, is that right?

Because unless you are able to SORT the keys of the array to a corresponding order for the fields in the group to match the field number, you will have a problem, as the keys for an array may be retrieved in any arbitrary order.
Yes you have right.
field 1 = NameOne
field 2 NameTwo
etc.
No problem about the sorting.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Repat on variable

Post by SparkOut » Sat Mar 28, 2015 7:47 pm

OK I made a stack to demonstrate, it's full of warnings about the sort ordering :wink:

But if that's not a problem, add a line to sort the keys and you should be ok.

(Unzip the downloaded stack first, of course! )
Attachments
groupfieldnametest.livecode.zip
(1.41 KiB) Downloaded 177 times

brandcode
Posts: 28
Joined: Mon Dec 01, 2014 12:28 pm

Re: Repat on variable

Post by brandcode » Sat Mar 28, 2015 7:51 pm

thank you guys really appreciate the help.
SparkOut thank you for the file.i will download it now and i test it.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Repat on variable

Post by SparkOut » Sat Mar 28, 2015 7:59 pm

Let us know how you get on

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”