Re: Sorting IP addresses
Re: Sorting IP addresses
- Subject: Re: Sorting IP addresses
- From: Graff <email@hidden>
- Date: Thu, 18 Dec 2003 16:44:17 -0500
Here is a solution which takes a file, sorts the IP addresses and saves
them to another file. The file is saved on the Desktop and is named
after the property FILE_NAME. If the file already exists the script
will add "- 1", "- 2", etc. to the name:
----------
property FILE_NAME : "sorted"
set theFile to choose file
try
set openedFile to open for access theFile
set theEOF to get eof openedFile
set theData to read openedFile for theEOF
close access theData
on error
display dialog "There was an error reading from " & (theFile as text)
close access theData
end try
set theList to every paragraph of theData
set numberList to {}
set {savedDelims, text item delimiters} to {text item delimiters, "."}
repeat with theItem in theList
set qOne to (text item 1 of theItem) as number
set qTwo to (text item 2 of theItem) as number
set qThree to (text item 3 of theItem) as number
set qFour to (text item 4 of theItem) as number
set numForm to (qOne * (256 ^ 3)) + (qTwo * (256 ^ 2)) + (qThree *
256) + qFour
copy numForm to end of numberList
-- display dialog theItem & return & numForm
end repeat
set text item delimiters to savedDelims
set sortedNumberList to sort(numberList)
set ipList to {}
repeat with theItem in sortedNumberList
set nOne to theItem div (256 ^ 3)
set nTwo to (theItem mod (256 ^ 3)) div (256 ^ 2)
set nThree to (theItem mod (256 ^ 2)) div 256
set nFour to theItem mod 256
set ipString to ((nOne as integer) as text) & "." & ((nTwo as integer)
as text) & "." & ((nThree as integer) as text) & "." & ((nFour as
integer) as text)
copy ipString to end of ipList
end repeat
set baseWritefile to ((path to desktop) as text) & FILE_NAME
set writeFile to baseWritefile & ".txt"
set attemptNumber to 0
tell application "Finder"
repeat while (exists file writeFile)
set attemptNumber to attemptNumber + 1
set writeFile to baseWritefile & " - " & ((attemptNumber) as text) &
".txt"
end repeat
end tell
try
set openedFile to open for access file writeFile with write permission
set {savedDelims, text item delimiters} to {text item delimiters,
return}
write (ipList as string) to openedFile
set text item delimiters to savedDelims
close access openedFile
on error
display dialog "There was an error writing to" & writeFile
close access openedFile
end try
on sort(listToSort)
if length of listToSort = 1 then return listToSort
set firstItem to item 1 of listToSort
set restOfList to rest of listToSort
set list1 to {}
set list2 to {}
repeat with currItem in restOfList
if firstItem < currItem then
set list2 to list2 & currItem
else
set list1 to list1 & currItem
end if
end repeat
if length of list1 > 1 then set list1 to sort(list1)
if length of list2 > 1 then set list2 to sort(list2)
return list1 & firstItem & list2
end sort
----------
On Dec 18, 2003, at 11:40 AM, Marconi wrote:
The answers thus far have not actually addresses the sorting but
rather getting the IP addresses into a 'sortable' form. That is,
rather than sort four times -- once for each quad -- convert the IPs
into a more easily sorted form and sort just once. Of course, I need
to preserve the original IPs as well, for display and writing to a
file, so it looks like the consensus is:
(Starting with a file of IP addresses, one per line, somewhere between
500-1000 lines...)
1) Read the file into an array. We'll make the unchanged IP address
the first element in each record of the array.
2) Traverse the array converting the IP address to a more sortable
form. Put that version into the respective second element in each
record.
It has been suggested to convert to ASCII text characters and to
convert to the decimal equivalent of the IP address. I would note that
using ASCII means that I would need a case-sensitive sort, yes? Might
be easier to stick with numbers.
3) sort the array based on the second element.
4) Write the first element of each array record back to the file.
Sound workable?
I have very limited AS skills and have done no scripting arrays at
all. Did all sorts of such things in Pascal, years ago.
Anyone care to point me to a good source of info on using arrays in AS?
Oh, could use an example of sorting too....
_______________________________________________
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.