Iterate over md Array (the Ruby way)

P

Peter v. N.

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 (e-mail address removed)
for j in 0...@vindex.length
@array[j] = @source[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
 
D

David A. Black

Hi --

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 (e-mail address removed)
for j in 0...@vindex.length
@array[j] = @source[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?

Also, what would constitute a non-convertible substring?

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


David
 
P

Peter v. N.

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

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

0000022340
0023457000
0000000110
0008000000
0000090000
0000330000
0000000730
0664000000
0034000000

or maybe:

AGCTAGCT12345
AAA3BBBCC
AAACC

@source[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...
Hi --

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 (e-mail address removed)
for j in 0...@vindex.length
@array[j] = @source[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?

Also, what would constitute a non-convertible substring?

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


David
 
D

David A. Black

Hi --

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

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

0000022340
0023457000
0000000110
0008000000
0000090000
0000330000
0000000730
0664000000
0034000000

or maybe:

AGCTAGCT12345
AAA3BBBCC
AAACC

@source[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
array.each {|s| s.replace(s.rjust(max,"0")).gsub!(/\D/,"0") }
puts array


David
Hi --

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 (e-mail address removed)
for j in 0...@vindex.length
@array[j] = @source[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?

Also, what would constitute a non-convertible substring?

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


David

 
A

Ara.T.Howard

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 (e-mail address removed)
for j in 0...@vindex.length
@array[j] = @source[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
--
===============================================================================
| 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
===============================================================================
 
W

William James

David said:
Hi --

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

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

0000022340
0023457000
0000000110
0008000000
0000090000
0000330000
0000000730
0664000000
0034000000

or maybe:

AGCTAGCT12345
AAA3BBBCC
AAACC

@source[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.
 
B

Brian Schröder

See if this helps:

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

max =3D 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
 
J

James Edward Gray II

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 =20
HERDOCs. :) Works great:

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

James Edward Gray II
 
G

Gavin Kistner

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

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

Here's what I use; it allows for arbitrary indent level, and indented =20=

lines within the block:

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

if __FILE__ =3D=3D $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.=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top