row = 0
IO.foreach("file2" ) {
>line>
array_file2[row] = line.split("|")
row += 1
}
N = array_file1.length
array_file1.times(N) { |row|
if array_file1[row][1] == array_file2[row][2]
array_file2[row].each{
>col>
array_file1[row][array_file1i[row].length] = col
}
end
}
But I get Undefined Method
Logically, I want to
loop through row of array_file 1{
check to see if array_file1.OMEGA is equal to array_file2.OMEGA
loop through column
insert contents of array_file2 to end of array_file1
end loop
end loop
In understand that this represents named sets of integers that are to be
merged. Right?
The natural representation of that would be a hash containing sets.
There is a set class, but I use arrays here:
require 'pp'
sets = Hash.new { | h, k | h[k] = [] } # hash that contains a new array
# for every new key
%w(file1.txt file2).each do | filename |
File.open(filename) do | f |
names = f.gets.chop.split('|')
f.each do | line |
names.zip(line.chop.split('|')).each do | name, value |
sets[name] << value
end
end
end
end
pp sets
This code does no tabular printing, treats the "integers" as strings,
without excluding empty strings and doubles. But that could easily be
fixed.
In understand that this represents named sets of integers that are to be
merged. Right?
The natural representation of that would be a hash containing sets.
There is a set class, but I use arrays here:
require 'pp'
sets = Hash.new { | h, k | h[k] = [] } # hash that contains a newarray
# for every new key
%w(file1.txt file2).each do | filename |
File.open(filename) do | f |
names = f.gets.chop.split('|')
f.each do | line |
names.zip(line.chop.split('|')).each do | name, value |
sets[name] << value
end
end
end
end
pp sets
This code does no tabular printing, treats the "integers" as strings,
without excluding empty strings and doubles. But that could easily be
fixed.
Thanks! I did:
sets.keys.each{|k|
print k, "|"
}
print "\n"
You could just do
puts set.keys.join('|')
To have the value output you specified in you original post, is a little
more complicated.
Is there a format function which will remove "" and change , to | ?
Don't use pp for production use. Do your own formatting. Here's my
updated script:
,----[ sets.rb ]
#!/usr/bin/env ruby
sets = Hash.new { | h, k | h[k] = [] } # hash that contains a new array
# for every new key
%w(file1.txt file2).each do | filename |
File.open(filename) do | f |
names = f.gets.chop.split('|')
f.each do | line |
names.zip(line.chop.split('|')).each do | name, value |
sets[name] << value.to_i if value and value !~ /^\s*$/
end
end
end
end
sets.values.each { | a | a.uniq! }
puts sets.keys.map { | k | '%8s ' % k }.join('|')
rows = sets.values.map { | a | a.size }.max
(1..rows).zip(*sets.values) do | row |
row.shift
puts row.map { | v | if v then '%8s ' % v else ' '*9 end }.join('|')
end
To have the value output you specified in you original post, is a little
more complicated.
> Is there a format function which will remove "" and change , to | ?
Don't use pp for production use. Do your own formatting. Here's my
updated script:
,----[ sets.rb ]
> #!/usr/bin/env ruby
>
> sets = Hash.new { | h, k | h[k] = [] } # hash that contains a newarray
> # for every new key
> %w(file1.txt file2).each do | filename |
> File.open(filename) do | f |
> names = f.gets.chop.split('|')
> f.each do | line |
> names.zip(line.chop.split('|')).each do | name, value |
> sets[name] << value.to_i if value and value !~ /^\s*$/
> end
> end
> end
> end
>
> sets.values.each { | a | a.uniq! }
>
> puts sets.keys.map { | k | '%8s ' % k }.join('|')
>
> rows = sets.values.map { | a | a.size }.max
>
> (1..rows).zip(*sets.values) do | row |
> row.shift
> puts row.map { | v | if v then '%8s ' % v else ' '*9 end }.join('|')
> end
`----