Advertisement

07.01.2008 at 02:12PM PDT, ID: 23531708
[x]
Attachment Details

Ruby: Iterating over multidimensional array

Asked by Lupi05 in Ruby Scripting Language

Tags: Ruby

Hi experts,

I started working with with Ruby and this looks like a rather simple problem I think, but still couldn't solve it.

products = Array.new

doc.search( "//td[@class='foo']" ).each do |foo|
     products << foo.inner_html.split(',')
end

NB: search is a Hpricot (ruby gem) method that finds all <TD> where class='foo' in a previously loaded HTML document and returns its contents (inner_html).

Now this seems to yield an array of arrays (2D array).

e.g. if the HTML document contains
<td class='foo'>abc,def</td>
<td class='foo'>ghi,jkl</td>
<td class='foo'>mno,pqr</td>
<td class='foo'>stu,vwx</td>

then Array products would look like this:
[abc][def]
[ghi][jkl]
[mno][pqr]
[stu][vwx]

Now, what I want to do is use gsub with a regex on def, jkl, pqr, vwx.

Of course, this is just an example, and the actual array is much bigger.

How do I access those array fields? Maybe iterate over the array somehow?

Thanks for your help.

Start Free Trial
 
Loading Advertisement...
 
[+][-]07.01.2008 at 07:26PM PDT, ID: 21913010

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.02.2008 at 05:24AM PDT, ID: 21915536

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.02.2008 at 09:19AM PDT, ID: 21917995

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Ruby Scripting Language
Tags: Ruby
Sign Up Now!
Solution Provided By: wesgarrison
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628