Forum: Ruby Re: add to words syntaxes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
67bb4df2775f6a6b603347dce7119571?d=identicon&s=25 unknown (Guest)
on 2006-03-16 09:28
(Received via mailing list)
------------------------------------------------------------------------------
---------------

datafile="kest_kft.txt"  # insert correct name here
text=IO.readlines(datafile)  # read lines into Array

output_file="my_nicely_arranged_text_file.txt"  # insert correct name
here

class String
        def do_first_script
                #  insert tags here
                return  result
        end
        def do_second_script
#insert  tags here
                return  result
        end
end




f=File.new(output_file,"w")


# first alternative
# assuming that you'll always have the same one/two-column
# structure
text.each_with_index{|x,i|
        if i<5
                f.puts  x.do_first_script
        else
                f.puts  x.do_second_script
        end
}


# second alternative (recommended)
# use regexps -


# assuming that you separate columns by tabs - and use tabs
# for nothing else than separating columns

text.each{|x|
        if x=~/\t/
                f.puts  x.do_first_script
        else
                f.puts  x.do_second_script
        end
}




f.close
This topic is locked and can not be replied to.