Forum: Ruby Iterating through a database result

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
104fcd49d9f843ec98e40883c2fc2a0d?d=identicon&s=25 Dan Webb (danw85)
on 2008-10-23 17:32
(Received via mailing list)
Hi all,

I've stumbled again. I'm currently trying to iterate though a result
output from PostgreSQL. Here's the database; I've been able to query
that I am getting all 5 records, however I don't seem to be able to
iterate through the second column i.e. 1,2,3,4,5.



7494;"1";"The origin (0,0) is top left.";TRUE;"1.0";"NOTE: Question
imported from map";"Correct.";;

7494;"2";"The origin (0,0) is top
right.";FALSE;"-0.25";"";"Incorrect.";;

7494;"3";"The origin (0,0) is bottom
left.";FALSE;"-0.25";"";"Incorrect.";;

7494;"4";"The origin (0,0) is bottom
right.";FALSE;"-0.25";"";"Incorrect.";;

7494;"5";"The origin (0,0) is located in the centre of the
screen.";FALSE;"-0.25";"";"Incorrect.";;





What I'm currently using is:

  num_options = res_qo.to_a.length

  puts num_options

  puts res_qo.to_a

  for i in (1..num_options)

    puts res_qo.to_ary[0][1]

  end



The result is an array of arrays by the looks of it (unless someone can
point out I've got the wrong end of the stick), but I can't move on from
the first record and am currently getting:

1

1

1

1

1



Any ideas would be great, as this should be one of the last problems I
have to sort out for this migration script.



Cheers,

Dan
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 Hugh Sasse (Guest)
on 2008-10-23 17:38
(Received via mailing list)
On Fri, 24 Oct 2008, Daniel Malcolm Webb [dbw] wrote:

> imported from map";"Correct.";;
>
> 7494;"2";"The origin (0,0) is top
> right.";FALSE;"-0.25";"";"Incorrect.";;
>
        [...]
>
>     puts res_qo.to_ary[0][1]
      puts res_qo.to_a[i][1]
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2008-10-23 17:53
(Received via mailing list)
On Thu, Oct 23, 2008 at 11:15 PM, Daniel Malcolm Webb [dbw]
<dbw@aber.ac.uk> wrote:
>...
> What I'm currently using is:
>  num_options = res_qo.to_a.length
>  puts num_options
>  puts res_qo.to_a
>  for i in (1..num_options)
>    puts res_qo.to_ary[0][1]
                                ^^^^^^^
i think that should be [i][1]
>  end

also, you are reconverting req_qo to array in each loop when in fact
you've already created it at the beginning

try

res_qo.to_a.each do |row|
    puts row[1]
end

kind regards -botp
104fcd49d9f843ec98e40883c2fc2a0d?d=identicon&s=25 Dan Webb (danw85)
on 2008-10-24 10:20
ah brilliant that worked. Thank you very much :)
This topic is locked and can not be replied to.