[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Please help with fixing output

lionbarrage

3/5/2009 7:23:00 PM

Hi all,

I'm having trouble with a hash output.

The original files are:
File 1
ALPHA|OMEGA|GAMMA
1|2|3
4|5|6

File 2
EPSILON|GREEK|OMEGA|BETA
7|8|9|0
12||13|
10|11|5|15


and I'm using the following code:

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

but the output seems a little strange and I'm not sure why that is.

Output should be
ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA
1 | 2 | 3 | | |
4 | 5 | 6 | 10 | 11 | 15
| 9 | | 7 | 8 | 0
| 13 | | 12 | |

instead it gets:
ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA
1 | 2 | 3 | 7 | 8 | 0
4 | 5 | 6 | 12 | 11 | 15
| 9 | | 10 | |
| 13 | | | |

Please advise on what's going on.

Thanks
5 Answers

WilliamJames

3/5/2009 8:51:00 PM

0

lionbarrage wrote:

> Hi all,
>
> I'm having trouble with a hash output.
>
> The original files are:
> File 1
> ALPHA|OMEGA|GAMMA
> 1|2|3
> 4|5|6
>
> File 2
> EPSILON|GREEK|OMEGA|BETA
> 7|8|9|0
> 12||13|
> 10|11|5|15
>
>
> and I'm using the following code:
>
> 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
>
> but the output seems a little strange and I'm not sure why that is.
>
> Output should be
> ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA
> 1 | 2 | 3 | | |
> 4 | 5 | 6 | 10 | 11 | 15
> | 9 | | 7 | 8 | 0
> | 13 | | 12 | |
>
> instead it gets:
> ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA
> 1 | 2 | 3 | 7 | 8 | 0
> 4 | 5 | 6 | 12 | 11 | 15
> | 9 | | 10 | |
> | 13 | | | |
>
> Please advise on what's going on.
>
> Thanks

Try this.


sets = Hash.new{ |h, k| h[k] = [] }
the_keys = []
row = 0

%w(file1.txt file2).each{ |filename|
File.open(filename){ |f|
names = f.gets.strip.split('|')
the_keys << names
f.each{ |line|
line.strip.split("|").each_with_index{|v,i|
sets[ names[i] ][ row ] = v.strip
}
row += 1
}
}
}

the_keys = the_keys.flatten.uniq

sets.values.each{ |a| a.uniq! }

puts the_keys.map{ |k| '%8s ' % k }.join('|')

rows = sets.values.map{ |a| a.size }.max

rows.times{|i|
puts the_keys.map{|k| v = sets[k][i] || ""
"%8s " % v }.join( "|" )
}

lionbarrage

3/5/2009 11:24:00 PM

0

On Mar 5, 12:51 pm, "William James" <> wrote:
> lionbarrage wrote:
> > Hi all,
>
> > I'm having trouble with a hash output.
>
> > The original files are:
> > File 1
> > ALPHA|OMEGA|GAMMA
> > 1|2|3
> > 4|5|6
>
> > File 2
> > EPSILON|GREEK|OMEGA|BETA
> > 7|8|9|0
> > 12||13|
> > 10|11|5|15
>
> > and I'm using the following code:
>
> >  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
>
> > but the output seems a little strange and I'm not sure why that is.
>
> > Output should be
> >    ALPHA | OMEGA |   GAMMA | EPSILON |   GREEK |    BETA
> >        1 |       2 |       3 |         |         |
> >        4 |       5 |       6 |    10   |      11 |       15
> >          |       9 |         |      7  |       8 |        0
> >          |      13 |         |      12 |         |
>
> > instead it gets:
> >    ALPHA |   OMEGA |   GAMMA | EPSILON |   GREEK |    BETA
> >        1 |       2 |       3 |       7 |       8 |       0
> >        4 |       5 |       6 |      12 |      11 |      15
> >           |       9 |         |      10 |         |
> >           |      13 |         |         |         |
>
> > Please advise on what's going on.
>
> > Thanks
>
> Try this.
>
> sets = Hash.new{ |h, k| h[k] = [] }
> the_keys = []
> row = 0
>
> %w(file1.txt file2).each{ |filename|
>   File.open(filename){ |f|
>     names = f.gets.strip.split('|')
>     the_keys << names
>     f.each{ |line|
>       line.strip.split("|").each_with_index{|v,i|
>         sets[ names[i] ][ row ] = v.strip
>       }
>       row += 1
>     }
>   }
>
> }
>
> the_keys = the_keys.flatten.uniq
>
> sets.values.each{ |a| a.uniq! }
>
> puts the_keys.map{ |k| '%8s ' % k }.join('|')
>
> rows = sets.values.map{ |a| a.size }.max
>
> rows.times{|i|
>   puts the_keys.map{|k| v = sets[k][i] || ""
>     "%8s " % v }.join( "|" )
>
> }
>
>
Unfortunately no luck, it did the first row right, but the 2nd row is
supposed to contain 4,5,6,10,11,15 . 7.8.9 should have |||7|8|9


ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA
1 | 2 | 3 | | |
4 | 5 | 6 | 7 | 8 | 0
| 9 | | 12 | | 15
| 13 | | 10 | 11 |

William James

3/6/2009 12:43:00 AM

0

lionbarrage wrote:

> The original files are:
> File 1
> ALPHA|OMEGA|GAMMA
> 1|2|3
> 4|5|6
>
> File 2
> EPSILON|GREEK|OMEGA|BETA
> 7|8|9|0
> 12||13|
> 10|11|5|15
>
>
> and I'm using the following code:
>
> 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
>
> but the output seems a little strange and I'm not sure why that is.
>
> Output should be
> ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA
> 1 | 2 | 3 | | |
> 4 | 5 | 6 | 10 | 11 | 15
> | 9 | | 7 | 8 | 0
> | 13 | | 12 | |

This doesn't look right. Consider the Epsilon column.
In the file the column is

7
12
10

And you want the output to be

10
7
12

lionbarrage

3/6/2009 5:04:00 PM

0

On Mar 5, 4:43 pm, "William James" <w_a_x_...@yahoo.com> wrote:
> lionbarrage wrote:
> > The original files are:
> > File 1
> > ALPHA|OMEGA|GAMMA
> > 1|2|3
> > 4|5|6
>
> > File 2
> > EPSILON|GREEK|OMEGA|BETA
> > 7|8|9|0
> > 12||13|
> > 10|11|5|15
>
> > and I'm using the following code:
>
> >  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
>
> > but the output seems a little strange and I'm not sure why that is.
>
> > Output should be
> >    ALPHA | OMEGA |   GAMMA | EPSILON |   GREEK |    BETA
> >        1 |       2 |       3 |         |         |
> >        4 |       5 |       6 |    10   |      11 |       15
> >          |       9 |         |      7  |       8 |        0
> >          |      13 |         |      12 |         |
>
> This doesn't look right.  Consider the Epsilon column.
> In the file the column is
>
> 7
> 12
> 10
>
> And you want the output to be
>
> 10
> 7
> 12

Ahh, I see where it causes confusion. The idea is to combine the two
columns using Omega.

So the row
ALPHA|OMEGA|GAMMA
4|5|6 in file 1 and the row
EPSILON|GREEK|OMEGA|BETA
10|11|5|15
in file 2 gets combined but the rest, who don't have a match in the
other file, does not.

lionbarrage

3/13/2009 5:01:00 PM

0

On Mar 6, 10:04 am, lionbarrage <cmakali...@gmail.com> wrote:
> On Mar 5, 4:43 pm, "William James" <w_a_x_...@yahoo.com> wrote:
>
>
>
> > lionbarrage wrote:
> > > The original files are:
> > > File 1
> > > ALPHA|OMEGA|GAMMA
> > > 1|2|3
> > > 4|5|6
>
> > > File 2
> > > EPSILON|GREEK|OMEGA|BETA
> > > 7|8|9|0
> > > 12||13|
> > > 10|11|5|15
>
> > > and I'm using the following code:
>
> > >  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
>
> > > but the output seems a little strange and I'm not sure why that is.
>
> > > Output should be
> > >    ALPHA | OMEGA |   GAMMA | EPSILON |   GREEK |    BETA
> > >        1 |       2 |       3 |         |         |
> > >        4 |       5 |       6 |    10   |      11 |       15
> > >          |       9 |         |      7  |       8 |        0
> > >          |      13 |         |      12 |         |
>
> > This doesn't look right.  Consider the Epsilon column.
> > In the file the column is
>
> > 7
> > 12
> > 10
>
> > And you want the output to be
>
> > 10
> > 7
> > 12
>
> Ahh, I see where it causes confusion.  The idea is to combine the two
> columns using Omega.
>
> So the row
> ALPHA|OMEGA|GAMMA
> 4|5|6               in file 1 and the row
> EPSILON|GREEK|OMEGA|BETA
> 10|11|5|15
> in file 2 gets combined but the rest, who don't have a match in the
> other file, does not.

I am still confused on how to do this. Tried using SQL Lite but Full
outer joins are not supported. Any ideas would be great!

Thanks!