Re: Sorting IP addresses
Re: Sorting IP addresses
- Subject: Re: Sorting IP addresses
- From: Graff <email@hidden>
- Date: Wed, 17 Dec 2003 13:47:52 -0500
You can't directly bit-shift but you can multiply, which ends up being
basically the same thing:
------------
set p to "123.12.31.250"
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set quadOne to (text item 1 of p) as number
set quadTwo to (text item 2 of p) as number
set quadThree to (text item 3 of p) as number
set quadFour to (text item 4 of p) as number
set pNumber to (quadOne * (8 ^ 3)) + (quadTwo * (8 ^ 2)) + (quadThree *
8) + quadFour
set AppleScript's text item delimiters to savedDelimiters
------------
On Dec 17, 2003, at 12:26 PM, Steve Mills wrote:
On Dec 17, 2003, at 8:37 AM, 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.
Right on the delimiter part. But I think a better way to sort them
would be as one number. Since each part of an address can be in the
range of 0-255, that means each part can fit into a byte. Put the 4
bytes together and you have a long. Sorting a single long should be
much easier than sorting 4 subparts.
So if you turn each IP into a list of 4 strings, you can turn each
list into a number like this bit of AppleScript/C mixture:
((item 1 of p) as number << 24) + ((item 2 of p) as number << 16) +
((item 3 of p) as number << 8) + (item 4 of p) as number
The hard part is doing the bit shifting in AS. Is that possible? I'm
tired, I just got up, and I'm on vacation, so my brain isn't up to
full speed.
_______________________________________________
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.