Arrays

R

Rajkumar Surabhi

Hi all,

i have an array contains no of elements. i want to concatinate 91 before
every elemnt of array. example

i have
A= [234,456,raju] like this
i want the out put arry tobe A=[91234,91456,91raju] like thissss

i want to add 91 before every element

how to do this
 
M

Mario Antonetti

[Note: parts of this message were removed to make it a legal post.]

Hi all,

i have an array contains no of elements. i want to concatinate 91 before
every elemnt of array. example

i have
A= [234,456,raju] like this
i want the out put arry tobe A=[91234,91456,91raju] like thissss

i want to add 91 before every element

how to do this
the assignment of the variable should look more like this:
 
R

Robert Klemme

2010/3/25 Brandon Jones said:
And the short form
a = ["123", "456", "raju"]
a.map!{|x|"91#{x}"} # => ["91123", "91456", "91raju"]

Even shorter (6 chars if I'm not mistaken):

a = %w{123 456 raju}
a.map!{|x|"91#{x}"}

;-)

Kind regards

robert
 
B

Brandon Jones

Robert said:
Even shorter (6 chars if I'm not mistaken):

a = %w{123 456 raju}
a.map!{|x|"91#{x}"}

;-)

Kind regards

robert

oh yeah?!? well take this

%w(123 456 raju).map{|x|"91#{x}"}

not exactly the same, as there is no persistent variable, but it is a
bit shorter
 
R

Robert Klemme

2010/3/25 Brandon Jones said:
oh yeah?!? well take this

%w(123 456 raju).map{|x|"91#{x}"}

not exactly the same, as there is no persistent variable, but it is a
bit shorter

You're absolutely right. My main point - which I failed to mention -
was, that arrays of strings can be easier represented with the %w
notation. This works of course only if strings need not contain white
space.

Kind regards

robert
 
J

jbw

[Note: parts of this message were removed to make it a legal post.]

You're absolutely right. My main point - which I failed to mention -
was, that arrays of strings can be easier represented with the %w
notation. This works of course only if strings need not contain white
space.

Kind regards

robert

I think %w[] would be more syntactically meaningful than curly
braces, square brackets remind us of
arrays. I think it is especially important when using shortcuts, which can
be obscure, to be as clear as possible.
 

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
474,159
Messages
2,570,879
Members
47,413
Latest member
ReeceDorri

Latest Threads

Top