# Array problem

**URL:** <https://www.ruby-forum.com/t/array-problem/131355>\
**Category:** Rails\
**Created:** [March 8, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/array-problem/131355 "2008-03-08T18:20:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 8, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/array-problem/131355/1 "2008-03-08T18:20:03Z")

</div>

Hi,

I need to parse a string char by char, if the char is a number it should  
go  
to a array without any modification, however in case of letters they  
should  
be decoded into hexadecimals before they go to the array, for example if  
I  
have:

text = “hello5” the array should be

=\> [“68”, “65”, “6c”, “6c”, “6f”, “5”]

So only letters should be converted.

I wrote the following code but don’t know what the problem with it:

text = “hello5”  
number = /\d/  
ntext = []

i = 0  
while i \< text.length  
if text[i].chr=~ number  
ntext \<\< text[i].chr  
else  
ntext \<\< text[i].chr.unpack(‘U’)[i].to\_s(16)  
end  
end

It seems to work for the first value only text[0], when replaced with  
text[1] or anything else I get:  
“in `to\_s’: wrong number of arguments (1 for 0) (ArgumentError)”

So the array ntext only stores the first converted letter, I couldn’t  
spot  
the problem?!!

Your help is really appreciated.

Regards

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 8, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/array-problem/131355/2 "2008-03-08T19:19:02Z")

</div>

Hi –

On Sat, 8 Mar 2008, Visit Indonesia 2008 wrote:

> Hi David,
> 
> what is the meaning of “%x” and also ’ “%x” % ’  
> Sorry I am so noob.

It’s a way of generating strings, using interpolated arguments. The  
target string has %-based format characters, like you’d use with  
sprintf. You need to provide one argument for each element in the  
format string.

So to print a string and a decimal, you’d do:

puts “%d is %s” % [10, “ten”] # 10 is ten

%x does a hex conversion:

puts “%x in hex is %s”, [10, “sixteen”] # a in hex is sixteen

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 8, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/array-problem/131355/3 "2008-03-08T19:09:57Z")

</div>

Hi David,

what is the meaning of “%x” and also ’ “%x” % ’  
Sorry I am so noob.

Thank you

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 8, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/array-problem/131355/4 "2008-03-08T18:46:01Z")

</div>

Hi –

On Sat, 8 Mar 2008, AN@S wrote:

> while i \< text.length  
> if text[i].chr=~ number  
> ntext \<\< text[i].chr  
> else  
> ntext \<\< text[i].chr.unpack(‘U’)[i].to\_s(16)  
> end  
> end

One problem with it is that you don’t increment i 🙂 The other  
problem is that you’ve got unpack(‘U’)[i] but you mean [0].

Keep in mind, though, that you almost never have to maintain a counter  
explicitly in Ruby, since the language gives you lots of ways to  
operate on collections. For example:

ntext = text.split(//).map {|char|  
case char  
when /\d/  
char  
else  
“%x” % char.unpack(‘U’)  
end  
}

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 8, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/array-problem/131355/5 "2008-03-08T21:11:32Z")

</div>

> ntext = text.split(//).map {|char|  
> case char  
> when /\d/  
> char  
> else  
> “%x” % char.unpack(‘U’)  
> end
> 
> }

Also as oneliner (1.8 and 1.9)

ntext = text.split(//).map {|char| char =~ /\d/ ? char : “%x” %  
char.unpack(“U”) }
