# Manipulating an Array Element

**URL:** <https://www.ruby-forum.com/t/manipulating-an-array-element/185664>\
**Category:** Ruby\
**Created:** [March 24, 2010, 11:07am UTC](https://www.ruby-forum.com/t/manipulating-an-array-element/185664 "2010-03-24T11:07:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [March 24, 2010, 11:07am UTC](https://www.ruby-forum.com/t/manipulating-an-array-element/185664/1 "2010-03-24T11:07:32Z")

</div>

Hi All,

I am saving some html code in an Array, I then want to extract certain  
values from the html code.

I have saved the html code into an Array but having difficulty  
manipulating the elements to extract the exact value that I require from  
the html code.

Here is the code:

#searches for images in the page which indicate platform number and  
stores them in an array  
platImages = (@doc/‘img[@width^=“54”]’)

```
#above search returns an unwanted image, so delete
platImages.pop
#puts platImages

#go through images names to get platform number
platImages.each { |img|
#convert element to string
            img.to_s

            #in this part, I have tried various string manipulation

```

methods to extract the values that i need, but doesn’t seem to work, it  
either returns nil for each element or throws an error

```
puts img
}

```

Any help is greatly appreciated.

Saeed.

---

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [March 24, 2010, 2:15pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-element/185664/2 "2010-03-24T14:15:47Z")

</div>

On Mar 24, 4:07 am, Saeed B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have saved the html code into an Array but having difficulty  
> manipulating the elements to extract the exact value that I require from  
> the html code.

What difficulty?

> ```
> #in this part, I have tried various string manipulation
> 
> ```
> 
> methods to extract the values that i need, but doesn’t seem to work, it  
> either returns nil for each element or throws an error

What do you want to do? What have you tried? What happened? Why wasn’t  
it what you wanted?

> Any help is greatly appreciated.

Provide more details, get more help 🙂

---

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [March 24, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-element/185664/3 "2010-03-24T14:28:45Z")

</div>

Gavin K. wrote:

> On Mar 24, 4:07ï¿½am, Saeed B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have saved the html code into an Array but having difficulty  
> > manipulating the elements to extract the exact value that I require from  
> > the html code.
> 
> What difficulty?
> 
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ #in this part, I have tried various string manipulation  
> > methods to extract the values that i need, but doesn’t seem to work, it  
> > either returns nil for each element or throws an error
> 
> What do you want to do? What have you tried? What happened? Why wasn’t  
> it what you wanted?
> 
> > Any help is greatly appreciated.
> 
> Provide more details, get more help 🙂

require ‘rubygems’  
require ‘hpricot’  
require ‘open-uri’  
#open the web page  
@doc= Hpricot(open(“[http://www.website.com](http://www.website.com)”))

```
#searches for images in the page and stores them in an array
images = (@doc/'img[@width^="54"]')

#puts images

#go through images names to get platform number
images.each { |img|
img.to_s
#extract characters one to four from image name
            img[0,4]
            puts img
}

```

However this returns the error ‘wrong number of arguments (2 for 1)  
(ArgumentError)’

Hope this make sense…

Saeed

---

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [March 24, 2010, 2:54pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-element/185664/4 "2010-03-24T14:54:26Z")

</div>

On Mar 24, 2010, at 9:28 AM, Saeed B. wrote:

> > > string manipulation  
> > > Provide more details, get more help 🙂
> 
> #puts images
> 
> #go through images names to get platform number  
> images.each { |img|  
> img.to\_s  
> #extract characters one to four from image name  
> img[0,4]

Assuming your problem is here^

img.to\_s does not convert the image to a string (img is a dom element,  
right?)

Try something like img.to\_s[0,4] or img[‘src’][0,4] (but please look  
in the documentation for Hpricot to see how to get the part you want  
as a string).

-Rob

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [March 24, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-element/185664/5 "2010-03-24T15:10:49Z")

</div>

Rob B. wrote:

> On Mar 24, 2010, at 9:28 AM, Saeed B. wrote:
> 
> > > > string manipulation  
> > > > Provide more details, get more help 🙂
> > 
> > #puts images
> > 
> > #go through images names to get platform number  
> > images.each { |img|  
> > img.to\_s  
> > #extract characters one to four from image name  
> > img[0,4]
> 
> Assuming your problem is here^
> 
> img.to\_s does not convert the image to a string (img is a dom element,  
> right?)
> 
> Try something like img.to\_s[0,4] or img[‘src’][0,4] (but please look  
> in the documentation for Hpricot to see how to get the part you want  
> as a string).
> 
> -Rob
> 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Thanks Rob, I did:  
platNo = img[‘src’].to\_s  
#extract the platform number from the image name  
puts platNo[1,1]

And it returns the value I need.

Once again, many thanks.

Saeed
