Iterate over md Array (the Ruby way)

Hi all,

Once again I disturb you for a, maybe, obvious thing
I can't figure out.

I have successfully created an md array (see older posts
from me) thanks to the support found here.

Now I would like to list all String items in the source array
(a Text file containing numbers) and convert and place them into the destination array as int objs. I know about the String#split method but this recreates an array that does not match the dimensions I want
(e.g. lines with different length).

My approach is very c-ish:

<code>
# @source is a String array. If the string contains a non-convertible
# substring, 0 is placed in destination array. @array is the destination # md array created with the appropriate dimensions according to @source # (longest line as vindex (-) and index as hindex (|) )

for i in 0...@hindex.length
  for j in 0...@vindex[i].length
    @array[i][j] = @source[i][j].to_i.char.to_i
  end
end
</code>

Is there a Ruby way to replace <code> with blocks?

Thank you very much for a hint.

Brgds,
Peter

Hi Peter,

My approach is very c-ish:

I don't know if someone already told you about Martin Fowler article "CollectionClosureMethod"

It is very informative and easy to understand for me. It shows nicely the different ways c-ish styles compare to The Ruby Way. Maybe this is of help. Forgive the noise if someone already hinted this.

Sascha Ebach

Hi --

···

On Thu, 13 Oct 2005, Peter v. N. wrote:

Hi all,

Once again I disturb you for a, maybe, obvious thing
I can't figure out.

I have successfully created an md array (see older posts
from me) thanks to the support found here.

Now I would like to list all String items in the source array
(a Text file containing numbers) and convert and place them into the destination array as int objs. I know about the String#split method but this recreates an array that does not match the dimensions I want
(e.g. lines with different length).

My approach is very c-ish:

<code>
# @source is a String array. If the string contains a non-convertible
# substring, 0 is placed in destination array. @array is the destination # md array created with the appropriate dimensions according to @source # (longest line as vindex (-) and index as hindex (|) )

for i in 0...@hindex.length
  for j in 0...@vindex[i].length
    @array[i][j] = @source[i][j].to_i.char.to_i
  end
end
</code>

Is there a Ruby way to replace <code> with blocks?

I'm not quite sure what's what here. I thought @vindex was the length
of the longest string, but then what's @vindex[i]?

Also, what would constitute a non-convertible substring?

Can you clarify, or maybe provide a small example of desired
input/output?

David

--
David A. Black
dblack@wobblini.net

OK. That's an approach. I'll check this out!

Thank you all for your help

Peter

···

Hi all,

Once again I disturb you for a, maybe, obvious thing
I can't figure out.

I have successfully created an md array (see older posts
from me) thanks to the support found here.

Now I would like to list all String items in the source array
(a Text file containing numbers) and convert and place them into the destination array as int objs. I know about the String#split method but this recreates an array that does not match the dimensions I want
(e.g. lines with different length).

My approach is very c-ish:

<code>
# @source is a String array. If the string contains a non-convertible
# substring, 0 is placed in destination array. @array is the destination # md array created with the appropriate dimensions according to @source # (longest line as vindex (-) and index as hindex (|) )

for i in 0...@hindex.length
    for j in 0...@vindex[i].length
        @array[i][j] = @source[i][j].to_i.char.to_i
    end
end
</code>

Is there a Ruby way to replace <code> with blocks?

Thank you very much for a hint.

Brgds,
Peter

you want to be using narray i think:

   harp:~ > cat a.rb
   require 'narray'
   require 'stringio'

   sio =
     StringIO::new <<-txt
       1 0 1 0 1 0
       42 42 42

       forty-two
     txt

   na =
     NArray::int 6, 3

   j = 0
   sio.each do |line|
     row = line.strip.split(%r/\s+/).map{|s| s.to_i}
     next if row.empty?
     row.each_with_index{|n,i| na[i,j] = n}
     j += 1
   end

   p na

   3.times{|row| puts "row #{ row } => #{ na[true, row].to_a.inspect }"}

   6.times{|col| puts "col #{ col } => #{ na[col, true].to_a.inspect }"}

   harp:~ > ruby a.rb
   NArray.int(6,3):
   [ [ 1, 0, 1, 0, 1, 0 ],
     [ 42, 42, 42, 0, 0, 0 ],
     [ 0, 0, 0, 0, 0, 0 ] ]
   row 0 => [1, 0, 1, 0, 1, 0]
   row 1 => [42, 42, 42, 0, 0, 0]
   row 2 => [0, 0, 0, 0, 0, 0]
   col 0 => [1, 42, 0]
   col 1 => [0, 42, 0]
   col 2 => [1, 42, 0]
   col 3 => [0, 0, 0]
   col 4 => [1, 0, 0]
   col 5 => [0, 0, 0]

cheers.

-a

···

On Thu, 13 Oct 2005, Peter v. N. wrote:

Hi all,

Once again I disturb you for a, maybe, obvious thing
I can't figure out.

I have successfully created an md array (see older posts
from me) thanks to the support found here.

Now I would like to list all String items in the source array
(a Text file containing numbers) and convert and place them into the destination array as int objs. I know about the String#split method but this recreates an array that does not match the dimensions I want
(e.g. lines with different length).

My approach is very c-ish:

<code>
# @source is a String array. If the string contains a non-convertible
# substring, 0 is placed in destination array. @array is the destination # md array created with the appropriate dimensions according to @source # (longest line as vindex (-) and index as hindex (|) )

for i in 0...@hindex.length
  for j in 0...@vindex[i].length
    @array[i][j] = @source[i][j].to_i.char.to_i
  end
end
</code>

Is there a Ruby way to replace <code> with blocks?

Thank you very much for a hint.

Brgds,
Peter

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================

Yes, I admit that I was not very clear
(English's not my mother tongue.. :wink: ):

An input text file (@source) looks like this:

0000022340
0023457000
0000000110
0008000000
0000090000
0000330000
0000000730
0664000000
0034000000

or maybe:

AGCTAGCT12345
AAA3BBBCC
AAACC

@source[i][j] = ASCII substring at this position:
e.g.: val = @source[1][3] #-> "3"

by non-convertible I mean everything that cannot be
represented as an integer ("12345678A", "A".to_i # -> "0")

Array a) yields in Array a)

Array a) yields in 0000000012345
      0000000000000

I managed to obtain such structures, but not by the Ruby way...

David A. Black wrote:

···

Hi --

On Thu, 13 Oct 2005, Peter v. N. wrote:

Hi all,

Once again I disturb you for a, maybe, obvious thing
I can't figure out.

I have successfully created an md array (see older posts
from me) thanks to the support found here.

Now I would like to list all String items in the source array
(a Text file containing numbers) and convert and place them into the destination array as int objs. I know about the String#split method but this recreates an array that does not match the dimensions I want
(e.g. lines with different length).

My approach is very c-ish:

<code>
# @source is a String array. If the string contains a non-convertible
# substring, 0 is placed in destination array. @array is the destination # md array created with the appropriate dimensions according to @source # (longest line as vindex (-) and index as hindex (|) )

for i in 0...@hindex.length
    for j in 0...@vindex[i].length
        @array[i][j] = @source[i][j].to_i.char.to_i
    end
end
</code>

Is there a Ruby way to replace <code> with blocks?

I'm not quite sure what's what here. I thought @vindex was the length
of the longest string, but then what's @vindex[i]?

Also, what would constitute a non-convertible substring?

Can you clarify, or maybe provide a small example of desired
input/output?

David

Hi --

Yes, I admit that I was not very clear
(English's not my mother tongue.. :wink: ):

An input text file (@source) looks like this:

0000022340
0023457000
0000000110
0008000000
0000090000
0000330000
0000000730
0664000000
0034000000

or maybe:

AGCTAGCT12345
AAA3BBBCC
AAACC

@source[i][j] = ASCII substring at this position:
e.g.: val = @source[1][3] #-> "3"

by non-convertible I mean everything that cannot be
represented as an integer ("12345678A", "A".to_i # -> "0")

Array a) yields in Array a)

Array a) yields in 0000000012345
      0000000000000

I managed to obtain such structures, but not by the Ruby way...

See if this helps:

array = <<EOM.split(/\n/)
0000022340
0023457000
0000000110
AGCTAGCT12345
AAA3BBBCC
AAACC
EOM

max = array.map {|st| st.chomp.size }.max
array.each {|s| s.replace(s.rjust(max,"0")).gsub!(/\D/,"0") }
puts array

David

···

On Thu, 13 Oct 2005, Peter v. N. wrote:

David A. Black wrote:

Hi --

On Thu, 13 Oct 2005, Peter v. N. wrote:

Hi all,

Once again I disturb you for a, maybe, obvious thing
I can't figure out.

I have successfully created an md array (see older posts
from me) thanks to the support found here.

Now I would like to list all String items in the source array
(a Text file containing numbers) and convert and place them into the destination array as int objs. I know about the String#split method but this recreates an array that does not match the dimensions I want
(e.g. lines with different length).

My approach is very c-ish:

<code>
# @source is a String array. If the string contains a non-convertible
# substring, 0 is placed in destination array. @array is the destination # md array created with the appropriate dimensions according to @source # (longest line as vindex (-) and index as hindex (|) )

for i in 0...@hindex.length
    for j in 0...@vindex[i].length
        @array[i][j] = @source[i][j].to_i.char.to_i
    end
end
</code>

Is there a Ruby way to replace <code> with blocks?

I'm not quite sure what's what here. I thought @vindex was the length
of the longest string, but then what's @vindex[i]?

Also, what would constitute a non-convertible substring?

Can you clarify, or maybe provide a small example of desired
input/output?

David

--
David A. Black
dblack@wobblini.net

David A. Black wrote:

Hi --

> Yes, I admit that I was not very clear
> (English's not my mother tongue.. :wink: ):
>
> An input text file (@source) looks like this:
>
> 0000022340
> 0023457000
> 0000000110
> 0008000000
> 0000090000
> 0000330000
> 0000000730
> 0664000000
> 0034000000
>
> or maybe:
>
> AGCTAGCT12345
> AAA3BBBCC
> AAACC
>
> @source[i][j] = ASCII substring at this position:
> e.g.: val = @source[1][3] #-> "3"
>
> by non-convertible I mean everything that cannot be
> represented as an integer ("12345678A", "A".to_i # -> "0")
>
> Array a) yields in Array a)
>
> Array a) yields in 0000000012345
> 0000000000000
> 0000000000000
>
> I managed to obtain such structures, but not by the Ruby way...

See if this helps:

array = <<EOM.split(/\n/)
0000022340
0023457000
0000000110
AGCTAGCT12345
AAA3BBBCC
AAACC
EOM

max = array.map {|st| st.chomp.size }.max

A minor point: since #split(/\n/) was used, #chomp isn't needed.

···

On Thu, 13 Oct 2005, Peter v. N. wrote:

array.each {|s| s.replace(s.rjust(max,"0")).gsub!(/\D/,"0") }
puts array

See if this helps:

array = <<EOM.split(/\n/)
0000022340
0023457000
0000000110
AGCTAGCT12345
AAA3BBBCC
AAACC
EOM

max = array.map {|st| st.chomp.size }.max
array.each {|s| s.replace(s.rjust(max,"0")).gsub!(/\D/,"0") }
puts array

David

Hello David,

that is cool. I did not know that it was possible to chain methods to
a herdoc like this.

thanks for the enlightenment,

best regards,

Brian

···

--
David A. Black
dblack@wobblini.net

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

I use that a lot, because I'm very anal and insist on indenting my HERDOCs. :slight_smile: Works great:

data = <<END.gsub(/^ /, "")
   indented
   lines
   go
   here
END

James Edward Gray II

···

On Oct 16, 2005, at 2:56 PM, Brian Schröder wrote:

that is cool. I did not know that it was possible to chain methods to
a herdoc like this.

Here's what I use; it allows for arbitrary indent level, and indented lines within the block:

[Sliver:~/Desktop] gkistner$ cat strip_indent.rb
class String
   def strip_indent
     (new_str = self.dup).strip_indent!
     new_str
   end
   def strip_indent!
     if leading_indent = self[ /\A\s*^([ \t]+)\S/, 1 ]
       self.gsub!( /^#{leading_indent}/, '' )
     end
   end
end

if __FILE__ == $0
   puts <<-ENDTEXT.strip_indent
     It's the end of the world
       (as we know it)
     and I feel fine.
   ENDTEXT
end

[Sliver:~/Desktop] gkistner$ ruby strip_indent.rb
It's the end of the world
   (as we know it)
and I feel fine.

···

On Oct 16, 2005, at 2:02 PM, James Edward Gray II wrote:

On Oct 16, 2005, at 2:56 PM, Brian Schröder wrote:

that is cool. I did not know that it was possible to chain methods to
a herdoc like this.

I use that a lot, because I'm very anal and insist on indenting my HERDOCs. :slight_smile: Works great:

data = <<END.gsub(/^ /, "")
  indented
  lines
  go
  here
END