2 Replies - 642 Views - Last Post: 18 October 2013 - 08:17 AM Rate Topic: -----

#1 Flying_Otter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 11

Help Parsing a MIME file

Posted 17 October 2013 - 05:19 AM

So I have a MIME types file that I need to parse. Everything goes accordingly so far, except that I need it to continually loop until the user enters a "q" or "Q" to exit. I've attempted a "while" loop but I'm not sure I was using it correctly because the program just kept running. I also am having trouble printing out "No extensions found" if a mime type isn't found, such as "blah/blah". This IS homework so any advice that could be given without directly giving me the answer would be appreciated!

#Creating the initial hash that will store the file contents.
types = Hash.new


#Looping through file and adding the contents to the hash.
File.open(ARGV[ 0 ], "r") do |f|
   f.each_line do |line|
   line.strip!
   next if line[0] == "#" or line.empty?
   key, value = line.split(/\W+\s/)
   types[key] = value
  end

#Getting the filename from command line...
fileName = File.basename(ARGV[ 0 ])
   puts "Processing #{fileName}..."
   puts "Finished processing."
   print "Enter a mimetype: "
   mimetypes = $stdin.gets.chomp

#Checking for matches.
types.each do |k, v|
     next if mimetypes != k
      if (v.nil?) 
          puts "No extensions found."	  
	  elsif mimetypes = k
	     puts "Extensions: #{v}"
	  elsif mimetypes = "q" || "Q"
		       exit
	  else
	      puts "No extensions found."
		end		
    end	
end



Is This A Good Question/Topic? 0
  • +

Replies To: Help Parsing a MIME file

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,041
  • Joined: 21-June 11

Re: Help Parsing a MIME file

Posted 17 October 2013 - 06:15 AM

If mimetypes != k, it will jump to the next iteration of the loop. If mimetypes is "q" or "Q", it will not be equal to k, so it will jump to the next iteration of the loop. So your if will never be reached. I'm not sure why it's even inside the each at all.

Also note that = is assignment and == is equality and that "q" || "Q" evaluates to just "q". To check whether mimetypes is equal to "q" or "Q", you should do mimetypes == "q" || mimetypes == "Q".

PS: If you want to find the extension for a given mimetype, you should just do types[mimetype]. Iterating over the whole hash and checking whether the key is equal for each entry, kind of defeats the purpose of having a hash map in the first place.

This post has been edited by sepp2k: 17 October 2013 - 06:17 AM

Was This Post Helpful? 1
  • +
  • -

#3 Flying_Otter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 11

Re: Help Parsing a MIME file

Posted 18 October 2013 - 08:17 AM

Sepp,
I appreciate the reply! Your answer made sense and I think I know what to do. I'll try it a little bit later today and let you know if I have any other issues. Again, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1