LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2006, 07:49 PM   #1
hedpe
Member
 
Registered: Jan 2005
Location: Boston, MA
Distribution: Debian
Posts: 380

Rep: Reputation: 30
cleaning the readability of this ruby script


Hey guys,

I could really use some help cleaning the readability of this ruby script. I could not figure out how to read files more properly for my situation. I know there is a fh.each_line ... but I'm reading more into blocks within the file... first here is the script:

http://thorium-ini.ini.cmu.edu/unique_contact.rb (md5sum 1742e0c0fc2022ca06cd99f07342d1c3)

Code:
#!/usr/bin/ruby
begin
	
	ip = Hash.new

	ARGV.each do |dir|
		files = Array.new				# Going to get all of the files in this dir which is all of the
		files = Dir.glob("#{dir}/**/*")		# 5 minute intervals in the day

		files.each do |file|		# for each file get the number of connections
			next if File.directory?(file)
			puts "#{file}"
			
			fh = File.open(file, "r")		# open the file for reading
			fh.readline		# skip the first four lines
			fh.readline
			fh.readline
			fh.readline

			while(1)
				ipaddr = fh.readline		# the first line of the block is the IP address
				ip[ipaddr] = Hash.new if(!ip[ipaddr])

				while(1)	# while we are inside the block of IP's they had contact with
					(dir, contact, count, loc) = fh.readline.split	# read the line in and split it
					break if(dir=="IP:")		# "IP:" denotes the end of the block
					ip[ipaddr][contact]=1		# The value doesn't matter, as long as the key exists
				end

					fh.readline 						#	skip blank line after block
					break if(fh.eof?)				# go to the next file if EOF
			end
			fh.close
		end
	end

	of = File.open("degree_timespan", "w")
	ip.each do |key, value|
		of.puts "#{key.chop} #{ip[key].length}"
	end
	of.close

end
To test it create a directory called "test" and put these two files in it:
http://thorium-ini.ini.cmu.edu/test1
http://thorium-ini.ini.cmu.edu/test2

Code:
ce0fc04cd448d645f0e1a259a4b40d16  /var/www/localhost/htdocs/test1
5532b17a93b51fbb5abd5b640245af8e /var/www/localhost/htdocs/test2
Then you can run it, and check your output with mine:
Code:
gnychis@x60s ~/school/thesis/host_analysis/degree $ ./unique_contact.rb test
test/test1
test/test2
gnychis@x60s ~/school/thesis/host_analysis/degree $ cat degree_timespan 
0.245.42.237 3
0.245.42.236 1
0.245.42.235 2
There must be a better way to reading in the file than the code I wrote.

To help you better understand the input:
Code:
0.245.42.235 *** TOP LEVEL HASH KEY ***
        <- 224.170.4.11 1 (inter)  *** HOST IT HAS CONTACTED ***
        <- 224.170.4.12 1 (inter)  *** HOST IT HAS CONTACTED ***
IP: 0.245.42.235 RA_Out: 0 ER_Out: 0 RA_In: 0 ER_In: 1 RA: 0 ER: 1 Total: 1  ** KNOW THE BLOCK ENDED*
Thanks!
George

Last edited by hedpe; 12-25-2006 at 07:53 PM.
 
Old 12-26-2006, 02:46 PM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
What do you mean by "readability"?

To me, it's the layout of the code on the page in a way that makes the logic easy to see.

However, "fh.each_line ... but I'm reading more into blocks within the file... " makes me think you have something else in mind.

Some things you could do that would improve my kind of "readability":
  • Ruthlessly limit line length to 72 characters, just like the old punch cards (the last 8 columns were often a serial/line # -- in case the deck got dropped).
  • Change your indent to 3 spaces rather than a tab.
  • Line up your comments in more regular columns.

Right now your script is a major pain to read because of the horizontal scrolling it causes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Subversion ruby script fails fenrisW0lf *BSD 1 12-21-2007 06:57 PM
openoffice.org math PDF conversion or readability skoot Linux - Desktop 0 08-29-2006 02:44 AM
Readability of posts cwwilson721 General 36 05-21-2006 04:18 PM
Cleaning up an old PC Merlyn Linux - Hardware 8 10-25-2005 06:33 AM
cleaning up? phonecian Linux - Software 1 10-19-2003 06:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration