• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Lists and Variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lists and Variables


  • Subject: Re: Lists and Variables
  • From: Deivy Petrescu <email@hidden>
  • Date: Fri, 16 May 2003 14:32:54 -0400

On Friday, May 16, 2003, at 11:20 US/Eastern, Larry Staton Jr. wrote:

> How does one go about assigning variables to a list of variable length?
>
> I have a variable length list of 1 to 10 strings. Each item of the
> list must be assigned to a variable named "TitleX", where X is item X
> of the list.
>
> Here's what I want to do in code:
>
> set tNames to {"bob", "sally", "alice", "timmy"}
>
> repeat with i from 1 to (count items in tNames)
> set Titlei to item i of tNames
> end repeat
>
> Result:
> Title1 --> "bob"
> Title2 --> "sally"
> Title3 --> "alice"
> Title4 --> "timmy"
>
> TitleX --> item X of tNames
>
> Unsurprisingly, the above does not compile.
>
> What I have hacked up is 10 "if" statements like this:
>
> if (count of tNames = 3) then
> set {Title1, Title2, Title3} to tNames
> end if
>
> if (count of tNames = 4) then
> set {Title1, Title2, Title3, Title4} to tNames
> end if
>
> if (count of tNames = X) then
> set {Title1 . . . TitleX} to tNames
> end if
>
> return Title1
> -- Result: "bob"
>
> Is there a more efficient way to create dynamic variables from a list
> of unknown length?
>
> TIA.
>
> --
> Larry Staton Jr.
>

Larry, this is how I'd go about doing that:

<script>
to usrf(theList)
script
{+class usrf;:theList}
end script
run script the result
end usrf


set tNames to {"bob", "sally", "alice", "timmy"}
set myrec to {}
repeat with i from 1 to (count items in tNames)
set end of myrec to ("Title " & i)
set end of myrec to item i of tNames
end repeat

set rec to usrf(myrec)

|Title 2| of rec -->"sally"

</script>
Regards

Deivy Petrescu
http://www.dicas.com/
_______________________________________________
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.

References: 
 >Lists and Variables (From: "Larry Staton Jr." <email@hidden>)

  • Prev by Date: Re: Run-Only script in OSX 10.2.6
  • Next by Date: Re: Run-Only script in OSX 10.2.6
  • Previous by thread: Lists and Variables
  • Next by thread: RE: Lists and Variables
  • Index(es):
    • Date
    • Thread