• 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: I want to convert a list of integers to their negative self
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: I want to convert a list of integers to their negative self


  • Subject: Re: I want to convert a list of integers to their negative self
  • From: John W Baxter <email@hidden>
  • Date: Thu, 1 Mar 2001 00:41:02 -0800

At 2:38 +0200 5/10/00, Jacco Rens wrote:
>Hi all,
>
>I want to convert a list of integers to their negative self
>
>{"1", "3", "7", "", ""}
>
>To:
>
>{"-1", "-3", "-7", "", ""}
>

I take the problem statement to be "For items in the list which are integer
values contained in strings, substitute a string with the negative of the
value; for other items, keep the item unchanged.

The following seems to be a way to do that:

-- Encoded with AppleScript Endec 1.0.3
to negateIntegers(aList)
set answer to {}
repeat with x in aList
set xx to contents of x
if class of xx is not string or xx is "" then
set end of answer to xx
else
try
set i to xx as integer
set end of answer to (-i) as string
on error
set end of answer to xx
end try
end if
end repeat
return answer
end negateIntegers

script spam
--just a script object
end script

set tests to {{"1", "3", "7", "", ""}, {spam, 1.2, 5, "four", "-77"}}
-- add your favorite test above
set solution to {}
repeat with t in tests
set end of solution to negateIntegers(t)
end repeat
solution

(End of encoded part)
solution ends up containing:
{
{
"-1",
"-3",
"-7",
"",
""
},
{
<<script spam>>,
1.2,
5,
"four",
"77"
}
}

--
John Baxter email@hidden Port Ludlow, WA, USA


  • Prev by Date: Re: Records
  • Next by Date: Re: list (form data) to Record
  • Previous by thread: Re: Where is the Searchable Applescript-users Archive?
  • Next by thread: Re: I want to convert a list of integers to their negative self
  • Index(es):
    • Date
    • Thread