Re: adding to a list selected from a list of lists
Re: adding to a list selected from a list of lists
- Subject: Re: adding to a list selected from a list of lists
- From: Ric Phillips <email@hidden>
- Date: Wed, 20 Feb 2002 10:16:22 +1100
On 19/2/02 9:39 PM, "paul withey" <email@hidden> wrote:
>
I would like a user to be able to add a text string to one of a group of
>
existing lists and thought of something along these lines would work...
>
>
list1 = {"1","2","3"}
>
list2 = {"3","4","5"]
>
the_lists = {"list1","list2"}
>
>
display dialog " what would you like to add ?" default answer ""
>
copy the text returned of the result as string to end of (choose from
>
list the_lists with prompt "where would you like to add the slice ?")
>
>
when run, with the user entering "test" and selecting list2 rather than
>
resulting in list2 = {"3","4","5","test"} as required,
>
the result is {"list2","test"}
>
>
where did it all go pete tong ?
>
Watch your data types, and remember that a variable name is not a string and
can't be referenced via one. The following will do what you want,
(Watch the line continuation character ",", which you get by typing
option+l)
set list1 to {"1", "2", "3"}
set list2 to {"3", "4", "5"}
set the_lists to {"list1", "list2"}
set answer1 to ,
the {text returned of (display dialog "What would you like to add?"
default answer "")}
set answer2 to choose from list the_lists with prompt ,
"Where would you like to add the slice ?" without multiple selections
allowed
if answer2 contains "list1" then
set list1 to list1 & answer2
else if answer2 contains "list2" then
set list2 to list2 & answer2
end if
Notes:
------------------
In the code the variable names list1 and list 2 are not the same as the
strings "list1" and "list2" contained in the list variable the_lists. A
logical test of the form list1 = "list1" will translate to <value of list1>
= "list1". Only if list1 was a string variable holding the value of "list1"
would the test return true.
Lists and strings are not the same data type and won't be coerced by a
logical comparisons. So if you test {"list1"} = "list1" it will return
false.
Remember {} indicates a list type. That's why the code uses the contains
test rather than "=". Choose from list returns a list - in the case above,
because the multiple selections allowed switch is set to false, only a
single item list can be returned, but it will still be a list. If you used
If answer2 = "test1" you would be actually testing a list against a string
and the result would always be false.
If you really wanted to use an "=" test you could cast the returned value of
the choose from list command to a string,
set answer2 to (choose from list <etc>) as string
Then,
If answer2 = "test1" etc...
Cheers,
Ric Phillips
Faculty Web Coordinator
Faculty of Humanities and Social Sciences
Latrobe University
Room HU3 324
Phone: 9479 2792
-----------------------------------------
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.