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

From: Christopher Howard <choward@indicium.us>
Date: Wed Aug 18 2010 - 06:24:08 AKDT

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

On 08/17/10 22:33, Arthur Corliss wrote:
> On Tue, 17 Aug 2010, Christopher Howard wrote:
>
>> 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.
>
> :-) Let's be fair, my last submission was literally only three lines of
> code. Count the semicolons! Besides, how readable is addressing a hash
> element like a two dimensional array?! ;-)
>
> Out of curiosity: is it possible to do CLI programs in ruby, or is the use
> of newlines & indentation necessary to delineate lines/blocks of code?
>

Yeah sure, like so:

  ruby -e "puts 'hi'; puts 'there'"

You /can/ use semicolons in ruby, you just don't /need/ to use them.

Also, indentation in Ruby is irrelevant to the interpreter. But most
Ruby programmers still indent for readability.

- --
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/

iEYEARECAAYFAkxr7QgACgkQQ5FLNdi0BcVRSQCdEdZOM6sV3olfj4zgG9Mo9B9k
pGMAnRfNbxdVkZiS8/y3SGvsZT9HUNj/
=j4gK
-----END PGP SIGNATURE-----
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed Aug 18 06:27:37 2010

This archive was generated by hypermail 2.1.8 : Wed Aug 18 2010 - 06:27:37 AKDT