简体   繁体   中英

read file into an array excluding the the commented out lines

I'm almost a Ruby-nOOb (have just the knowledge of Ruby to write some basic .erb template or Puppet custom-facts). Looks like my requirements fairly simple but can't get my head around it.

Trying to write a .erb template, where it reads a file (with space delimited lines) to an array and then handle each array element according to the requirements. This is what I got so far:

fname = "webURI.txt"

def myArray()
    #if defined? $fname
    if File.exist?($fname) and File.file?($fname)
        IO.readlines($fname)
    end
end

myArray.each_index do |i|
    myLine = myArray[i].split(' ')
    puts myLine[0] +"\t=> "+ myLine.last
end

Which works just fine, except (for obvious reason) for the line that is commented out or blank lines. I also want to make sure that when spitted (by space) up, the line shouldn't have more than two fields in it; a file like this:

# This is a COMMENT
#
# Puppet dashboard
puppet controller-all-local.example.co.uk:80

# Nagios monitoring
nagios controller-all-local.example.co.uk::80/nagios
tac talend-tac-local.example.co.uk:8080/org.talend.admin

mng console talend-mca-local.example.co.uk:8080/amc     # Line with three fields

So, basically these two things I'd like to achieve:

  1. Read the lines into array, stripping off everything after the first #
  2. Split each element and print a message if the number id more than two

Any help would be greatly appreciated. Cheers!!


Update 25/02

Thanks guy for your help!!

The blank thing doesn't work for at all; throwing in this error; but I kinda failed to understand why:

undefined method `blank?' for "\\n":String (NoMethodError)

The array: myArray , which I get is actually something like this (using p instead of puts :

 ["\\n", "puppet controller-all-local.example.co.uk:80\\n", "\\n", "\\n", "nagios controller-all-local.example.co.uk::80/nagios\\n", ..... \\n"] 

Hence, I had to do this to get around this prob:

 $fname = "webURI.txt" def myArray() if File.exist?($fname) and File.file?($fname) IO.readlines($fname).map { |arr| arr.gsub(/#.*/,'') } end end # remove blank lines SSS = myArray.reject { |ln| ln.start_with?("\\n") } SSS.each_index do |i| myLine = SSS[i].split(' ') if myLine.length > 2 puts "Too many arguments!!!" elsif myLine.length == 1 puts "page"+ i.to_s + "\\t=> " + myLine[0] else puts myLine[0] +"\\t=> "+ myLine.last end end 

You are most welcome to improve the code. cheers!!

goodArray = myArray.reject do |line|
  line.start_with?('#') || line.split(' ').length > 2
end

This would reject whatever that either starts with # or the split returns an array of more than two elements returning you an array of only good items.

Edit:

For your inline commenting you can then do

goodArray.map do |line|
  line.gsub(/#.*/, '')
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM