Carlos Delmar wrote:
http://rafb.net/paste/results/2yJH7J28.html
BUT: I still would like to remove ‘puts’ and ‘warn’ in the
process_transaction method and would much prefer to ‘puts’ a resulting
string later in the transactions.each statement (or write that to a
file).
Somehow I find your design a bit contrived: you store your meta data in
strings and then have to extract what you need. It would be much
cleaner
and simpler to directly store it, for example in arrays stored in
hashes:
#!/usr/bin/ruby
script should check if one of the keys in Ausgabekonten or
Einnahmekonten hash
can be found in each line of the transactions array.
If it does, it should print the matching key and value of the
Ausgabekonten hash.
If it does not, it should print a warning.
Ausgabekonten = {
/BIO COMPANY / => [“1805”, “Bio Company”, “Lebensmittel”],
/KAISERS / => [“1805”, “Bio Company”, “Lebensmittel”],
/VINOTRIA / => [“1805”, “Bio Company”, “Lebensmittel”],
}
Einnahmekonten = {
/CLIENT_ONE/ => [“8300”, “Client 1”, “Erlöse 7% Ust”],
/CLIENT_TWO/ => [“8300”, “Client 2”, “Erlöse 7% Ust”],
}
transactions = [
‘“30.12.2005” “30.12.2005” “VINOTRIA ITALIAN VINE STORE” “-20,00”
“EUR” “"',
'“30.12.2005” “30.12.2005” “KAISERS FOOD” “-140,00” “EUR” "”’,
‘“30.12.2005” “30.12.2005” “SOMETHING NEW” “900,00” “EUR” “"',
'“30.12.2005” “30.12.2005” “CLIENT_ONE Job XY” “2.000,00” “EUR”
"”’,
‘“30.12.2005” “30.12.2005” “I DONT REMEMBER” “-300,00” “EUR” “"',
'“30.12.2005” “30.12.2005” “CLIENT_TWO Job XY” “1.500,00” “EUR” "”’
]
method finds money snippet in string and converts it into a string
def find_money_string (string)
string.scan(/"-?[0123456789]{0,3}.[0123456789]{0,3},[0-9][0-9]"/).to_s.c
hop[1…-1]
end
method changes string from german format (,.) to float (.)
def format_number(string)
string.tr(“.”, “;”).tr(“,”, “.”).tr(“;”, “”).to_f
end
extracts the 4 digit number from a string
def extract_four_digit(string)
string.to_s.scan(/[0-9][0-9][0-9][0-9]/)
end
method extracts money of string
def get_money (string)
x = find_money_string(string)
format_number(x)
end
def parse(line)
line.scan(/“([^”]*)"/).map {|m| m[0]}
end
method checks what key matches with string and adds additional info to
each string
def process_transaction (line_of_file)
date1, date2, text, amount, currency, rest = parse line_of_file
amount = amount.tr(‘.,’, ‘_.’).to_f
if amount < 0
key, info = Ausgabekonten.find { |key, konto| key =~ line_of_file }
if info
print line_of_file.chomp, " "“, info[1], “" "”, info[0],
“1200\n”
else
warn line_of_file.chomp + " (no
– – --”
end
else
key, info = Einnahmekonten.find { |key, konto| key =~ line_of_file }
if info
print line_of_file.chomp, " "", info[1], "" "1200" “,
info[0],
“\n”
else
warn line_of_file.chomp + " (no
– – --”
end
end
end
apply process_transaction to each line of the transactions array
transactions.each do |member|
process_transaction(member)
end
HTH
robert