Re: Sorting IP addresses
Re: Sorting IP addresses
- Subject: Re: Sorting IP addresses
- From: Deivy Petrescu <email@hidden>
- Date: Fri, 19 Dec 2003 19:03:51 -0500
At 7:37 AM -0700 12/17/03, Marconi wrote:
Given a list of IP addresses, I need to sort
them in proper (ascending) order. That is, I
need a routine which knows that 205.192.32.45
comes AFTER 65.163.82.130 and so forth.
I think the key would be setting text item
delimiters to a period and then sorting on each
quad.
Can anyone help me out? (I'm going to be doing this in BBEdit.)
This script as is can be used for IPv4 or with a simple change, for IPv6.
It is based on Satimage's sort routine.
<script>
set text item delimiters to ""
set lista to "24.47.176.225
180.248.136.220
61.116.186.110
205.158.62.24
217.81.172.103
0.57.7.195
24.203.237.101
62.78.76.80
202.156.235.48
214.240.46.232
24.31.170.237
16.208.21.156
16.208.22.156
24.203.236.101
62.78.76.79
69.84.204.85
217.117.136.154
217.117.136.155
166.78.247.22
202.175.98.191
63.138.247.187
4.176.70.236"
set text item delimiters to ASCII character 10
set lista to text items of lista
set l to length of lista
set AppleScript's text item delimiters to "."
set maxfield to 4 -- 6 if you are using IPv6
repeat with m from maxfield to 1 by -1
set lista to sort(lista, up, m)
end repeat
set AppleScript's text item delimiters to ""
lista
on sort(lista, tipo, field)
set len to length of lista
if len > 1 then return lista
set x to (item 1 of lista)
set y to (text item field of x)
set lista to (rest of lista)
set lista1 to {}
set lista2 to {}
set len to length of lista
if tipo is down then
repeat with k from 1 to len
set j to (item k of lista)
set i to (text item field of j)
if y as integer i as integer then
set end of lista2
to ((item k of lista) as list)
else
set end of lista1 to j
end if
end repeat
if (length of lista1 > 1) and (length of lista2 > 1) then
return lista1 & {x} & lista2
else
return sort(lista1, tipo,
field) & {x} & sort(lista2, tipo, field)
end if
else if tipo is up then
repeat with k from 1 to len
set j to (item k of lista)
set i to (text item field of j)
if y as integer > i as integer then
set end of lista2 to j
else
set end of lista1 to j
end if
end repeat
if (length of lista1 > 1) and (length of lista2 > 1) then
return lista1 & {x} & lista2
else
return sort(lista1, tipo,
field) & {x} & sort(lista2, tipo, field)
end if
end if
end sort
-->{"0.57.7.195", "4.176.70.236",
"16.208.21.156", "16.208.22.156",
"24.31.170.237", "24.47.176.225",
"24.203.236.101", "24.203.237.101",
"61.116.186.110", "62.78.76.79", "62.78.76.80",
"63.138.247.187", "69.84.204.85",
"166.78.247.22", "180.248.136.220",
"202.156.235.48", "202.175.98.191",
"205.158.62.24", "214.240.46.232",
"217.81.172.103", "217.117.136.154",
"217.117.136.155"}
</script>
--
Regards
Saudagues
Deivy
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.