Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:02:17+00:00 2026-05-16T00:02:17+00:00

So I need to run a loop in Ruby to pass some strings into

  • 0

So I need to run a loop in Ruby to pass some strings into SQLite. Basically I have a table that looks like this:

    pID          Data
1649,1650,1651|Some data
1643,3|some more data
23,4,5,6,7|More data

Now in my SQLite queries, I will sometimes need to pass all the pIDs for a given line through as one whole string, which I can easily do with the ‘i’ variable in the loop below:

pID = db.execute( "select distinct pID from pmesh")
pID.each do |i|
end

Where ‘i’ will equal each pID line and each string looks like this:

1649,1650,1651
1643,3
23,4,5,6,7

But sometimes I will need each string to look like this for me to pass them through:

"1649","1650","1651"
"1643","3"
"23,"4","5","6","7"    

Is there any easy loop that I can do that will put quotations and proper commas to make the string appear as I want them to? I know I can do “i = i.to_s() and a = i.split(“,”)”, but I really don’t know much else loop-wise after that.

Thanks,
Bobby

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:02:17+00:00Added an answer on May 16, 2026 at 12:02 am

    Use Enumerable.collect

    Here’s an example:

    x = [1,2,4,5,6]
    => [1,2,4,5,6]
    x.collect { |i| "#{i}" }
    = > ["1","2","4","5","6"]
    

    or, if you want to join them into one string:

    x.collect { |i| "#{i}" }.join(', ')
    

    or, if your input is a csv string:

       x.split(",").collect { |i| "#{i}" }.join(', ')
    

    Edit:

    Here is an example of what I think you’re trying to do.

     require 'sqlite3'
     db = SQLite3::Database.new("test.db")
     db.execute("create table bobby (pID varchar2(50), Data varchar(100))")
     db.execute("select * from bobby")
     db.execute("insert into bobby values ( ? , ?)", "1649,1650,1651", "Some Data1")
     db.execute("insert into bobby values ( ? , ?)", "1643,3", "Some Data2")
     db.execute("insert into bobby values ( ? , ?)", "23,4,5,6,7", "Some Data3")
     db.execute("select * from bobby").each do |row|
        p
     end
    # outputs [["1649,1650,1651", "Some Data1"], ["1643,3", "Some Data2"], ["23,4,5,6,7", "Some Data3"]]
    
    db.results_as_hash = true #allows hashing row via column name
    
    puts "Each ID as array of strings"
     db.execute("select * from bobby").each do |row|
        p row['pID'].split(",").collect { |i| "#{i}" }
     end
    
    puts "Each ID as single string"
     db.execute("select * from bobby").each do |row|
        p row['pID'].split(",").collect { |i| "#{i}" }.join(", ")
     end
    

    Here is the output:

    C:\Users\Jim\Desktop>ruby sqlLiteExample.rb
    Each ID as array of strings
    ["1649", "1650", "1651"]
    ["1643", "3"]
    ["23", "4", "5", "6", "7"]
    Each ID as single string
    "1649, 1650, 1651"
    "1643, 3"
    "23, 4, 5, 6, 7"
    

    edit2:
    if you want your values to be a string of , for instance, “”1643”, “3””, then you’ll have to do the following:

    x.map{|i| '"' + "#{i}" + '"' }.join(',')
    # outputs "\"124\",\"5525\",\"23525\""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are using jQuery UI, then create your element… May 16, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer Whether it works depends on what class type and selectedBirdType… May 16, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer Apparently this is related to a bug in XCode 3.2… May 16, 2026 at 3:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm sure there's some trivial one-liner with perl, ruby, bash whatever that would let
I need to run a PHP loop for a total of 100, 000 times
I have an array(list?) in ruby: allRows = [start,one,two,start,three,four,start,five,six,seven,eight,start,nine,ten] I need to run a
I'm using IO.popen in Ruby to run a series of command line commands in
Do languages like e.g. Ruby (if running MRI, I mean not compiled to byte-code)
I have some experience with programming, but I have very little experience when it
I'm writing a few very tight loops and the outermost loop will run for
I have some basic questions about core data (which I am new to) and
I need to run an .sh file and get its output. I need to
I need to run an application in an embedded system continuously. While implementing this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.