0

For microarray data processing, I need to make a list of gene names from 1 to 654, like Gene_1 ... Gene_654.

My simple Ruby code produces the following:

1.upto(654).each { |i| print "Gene" } 

The result is:

GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene

GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene
..................................
GeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGeneGene=> 1
irb(main):008:0>

How do I add a "postfix _#" in sequential incremental order to a printed string and put them in a column, like:

Gene_1
Gene_2
::::::
Gene_654
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
KvasDub
  • 281
  • 7
  • 16
  • Please confirm that the edited question is displaying the desired output formatting correctly. The question's text showed the output as a single column, in multiple rows. There appears to be confusion among the other answerers who didn't realize that. – the Tin Man Sep 04 '13 at 19:11

3 Answers3

0
1.upto(654).each { |i| printf "%8s\t", "Gene_#{i}" }

Source: http://www.ruby-doc.org/core-2.0.0/Kernel.html#format-method

Américo Duarte
  • 525
  • 3
  • 16
0

Edited to conform to the new requirements:

1.upto(654).each { |i| puts "Gene_#{i}" } 

--output:--
Gene_1 
Gene_2 
...
Geen_654
7stud
  • 46,922
  • 14
  • 101
  • 127
0

I'd use:

str = 'Gene_0'
654.times { puts str.next! }

Which outputs:

Gene_1
...
Gene_654

If you need the text output to the same width, perhaps because you're going to append information to each line, use some formatting:

str = 'Gene_0'
654.times { puts '%8s ' % str.next! }
# >>   Gene_1 
...
# >>   Gene_9 
# >>  Gene_10 
...
# >>  Gene_99 
# >> Gene_100 
...
# >> Gene_654 

If you need columns across a page:

str = 'Gene_0'
654.times { print '%8s ' % str.next! }
puts

Which spaces them out in 8-space-wide columns.

By default %8s uses right alignment, which isn't always what you want. Instead you can use %-8s for left-alignment.

You can build an array containing the column headings:

str = 'Gene_0'
columns = []
654.times { columns << '%-8s' % str.next! }
puts columns.join(' ')

You could even use something like inject:

str = 'Gene_0'
columns = []
(1..654).inject(columns) { |a, i| a.push('%-8s' % str.next!) }
puts columns.join(' ')

But that starts to add code that doesn't really help.


The OP asked:

...how to add " " to the result...

The output above doesn't make it easy to see the whitespace automatically appended to the output by '%8s ', so I tweaked the format-string to make it more obvious by wrapping the output in double-quotes:

str = 'Gene_0'
654.times { puts '"%8s "' % str.next! }

And here's the corresponding output, trimmed down to show how the format string maintains the column width as the string value increments:

# >> "  Gene_1 "
...
# >> "  Gene_9 "
# >> " Gene_10 "
...
# >> " Gene_99 "
# >> "Gene_100 "
...
# >> "Gene_654 "

If you want all the white-space to occur at the end of the column, use a left-alignment:

str = 'Gene_0'
654.times { puts '"%-8s "' % str.next! }

Which outputs:

# >> "Gene_1   "
...
# >> "Gene_9   "
# >> "Gene_10  "
...
# >> "Gene_99  "
# >> "Gene_100 "
...
# >> "Gene_654 "
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Nice idea...You still need to make several corrections to get the output the op asked for. – 7stud Sep 04 '13 at 18:33
  • It would have helped if you'd have actually looked at the source text for his question. He wants his text in a single column, not multiple columns, but SO, and the browser turns it into columns when displaying the rendered text. – the Tin Man Sep 04 '13 at 19:03
  • Thank you, how to add " " to the result to make it like: "Gene_1" – KvasDub Sep 05 '13 at 08:56
  • Look at the source code in my answer, and read about print-formatting. `'%8s '` in a format string adds a trailing space, which I already showed. – the Tin Man Sep 05 '13 at 15:02
  • @KvasDub, I added an example at the bottom showing how the trailing whitespace is already there. – the Tin Man Sep 05 '13 at 15:12