Noob question on writing a script in ruby


#1

hello to everyone, i am new to ruby and programming. At the moment i am
a bit confused on how to make a program do what i will explain in the
following lines so please any help will be appreciated!

In a few words, what i want to is to write a script which will parse
many .txt files inside a folder. Each file contains text in the format:

FileID: id
Name1: Value
Name2: Value2

, where the Names remain the same in every file but the Values change
(id is number).

So, the program based on id (number) of each file will handle the file

for example if “id” is “1” it will skip the file.
if “id” is “10” it will process the file.
if “id” is “20” it will process the file.

When each file will be processed the program will have to compare the
other values (Value, Value2 etc) with the values of a database
(MS-Access db containing same values) and if the values are same do
nothing, if not do something else.

A possible solution im thinking may be:

require ‘DBI’

connection with database

cn = DBI.connect(‘DBI:ODBC:database’)

parsing each file inside the folder

class File_parse

basedir = “C:\filefolder”
Dir.chdir(basedir)
files = Dir.glob("*")

begin
files.each do |file|
result = {}
files.readlines do |lines|
k,v=line.split("=")
result[k.strip]=v.strip
end

but from this point i don’t know how to continue, how to compare the
values of each file with the values inside the database…

for example

if result.FileID=1
do nothing
set a flag to false

if result.FileID=10
compare Name1: Value1 with values of database
set a flag to true

if result.FileID=20
compare Name2: Value2 with values of database
set a flag to true

end

if flag = true
do something
else
do nothing
end

my programming skills are very limited so any help or explanations on
how to continue will be much appreciated.


#2

Hi Tom,

I don’t have script.But i will give simple idea to you.

  1. DB contains --> value1,value2,value3 …

  2. collect all the fields as an array @db=[“value1”,“value2”,“value3”]

  3. new record set -> “value2”,“value4”

  4. if @db.include?(“value2”)== false then
    SAVE DB
    else
    NEXT
    end

I feel it was not a good code. Anyway let us see this thread for new
ideas…

Thanks,
P.Raveendran