Bulk resolve IP addresses in Smalltalk
I’m sure bash and PowerShell geeks out there might have a better way, but since my day job is all about Smalltalk, the following seems very appropriate when it comes to translating a ton of IP addresses into domain names in a file for further review,
cache := Dictionary new.
rs := ('fwrules.20101110.raw.txt' asFilename withEncoding: #utf8)
readStream lineEndTransparent.
ws := ('fwrules.20101110.resolved.txt' asFilename withEncoding: #utf8)
writeStream lineEndTransparent.
['(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
, '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
, '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
, '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' asRegex
copyStream: rs
to: ws
translatingMatchesUsing:
[:str |
cache
at: str
ifAbsentPut:
[| ip |
ip := IPSocketAddress stringToBytes: str.
IPSocketAddress hostNameByAddress: ip]]]
ensure:
[rs close.
ws close].
Categories: Hints, Smalltalk, VisualWorks
You don’t need to validate the IP addresses (and doing it with a regex is a bad idea anyway… using a screwdriver as a hammer). So simplify your life and code with:
‘(\d+)\.\(d+)\.(\d+)\.(\d+)’ asRegex
Your fingers and maintenance programmers will thank you.
I don’t know, I prefer more code that’s more readable than regex voodoo with a ton of useless nerd joy.
Writing the right bit of regex can make you type less but can be time consuming to write but what’s worst, can be time consuming to read (so maintain) at a later time (say 18 months later).
Not to mention if that is going to be made by another guy that is seeing that for the first time.