[aklug] Re: perl lists question, sorting, sort of.

From: Christopher Howard <choward@indicium.us>
Date: Tue Aug 17 2010 - 21:00:40 AKDT

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/17/10 20:10, Arthur Corliss wrote:
> On Tue, 17 Aug 2010, Tim Gibney wrote:
>
>> That is alot of strange idiosyncratic babble I have ever seen to print a
>> simple hosts file I.P addresses with a sort. Wouldn't python ... or even
>> bash be easier? Perl scares me
>
> :-) Just two Perl dweebs geeking out, ignore it. But you're proposing a
> great challenge for the other language advocates here. Can anyone else here
> come up with a python/ruby/etc example with equivalent functionality? Any
> rules? No use of external (non-core) libraries?
>
> My original post was sufficient for your original request, it just got
> embellished with additional functionality. All told, I still think that
> what Perl can do on the CLI is pretty flexible and impressive. Let's see
> what the other languages can do!
>
> --Arthur Corliss
> Live Free or Die
> ---------
> To unsubscribe, send email to <aklug-request@aklug.org>
> with 'unsubscribe' in the message body.
>

Here is a start in Ruby:

#!/usr/bin/env ruby

@data = {}
File.new(ARGV[0]).each do |line|
  if line =~ /^\s*(\S+)\s*(\S+)\s*$/
    if !@data[$1]
      @data[$1] = {}
    end
    @data[$1][$2] = true
  end
end
@data.each do |ip, host_name_hash|
  print ip.to_s
  host_name_hash.each_key do |hostname|
    print " " + hostname.to_s
  end
  print "\n"
end

Call it like: ./hosts.rb olddatafile > newdatafile

Sorry, that was the two minute job, so we only do one hostname per IP
address in the original data file. (Gotta get back to working on my
church website...)

Also, unlike Arthur and those Perl dweebs (who think they are impressing
us by using semicolons to squeeze eighteen lines of code into six) I
decided to leave the code in a somewhat readable state.

- --
Christopher Howard
frigidcode.com
theologia.indicium.us
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkxraPgACgkQQ5FLNdi0BcW66ACfaB85yfZx8G6z58VtkKReLgTK
sIMAoJlkFLWdliNRKE+NfleJUclMU9gi
=EBdI
-----END PGP SIGNATURE-----
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue Aug 17 21:04:14 2010

This archive was generated by hypermail 2.1.8 : Tue Aug 17 2010 - 21:04:14 AKDT