Re: I want to convert a list of integers to their negative self
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