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 6364629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:11:21+00:00 2026-05-25T00:11:21+00:00

I’m trying to open a CSV file, look up a string, and then return

  • 0

I’m trying to open a CSV file, look up a string, and then return the 2nd column of the csv file, but only the the first instance of it. I’ve gotten as far as the following, but unfortunately, it returns every instance. I’m a bit flummoxed.

Can the gods of Ruby help? Thanks much in advance.

M

for the purpose of this example, let’s say names.csv is a file with the following:

foo, happy
foo, sad
bar, tired
foo, hungry
foo, bad


#!/usr/local/bin/ruby -w

require 'rubygems'
require 'fastercsv'
require 'pp'

  FasterCSV.open('newfile.csv', 'w') do |output|
    FasterCSV.foreach('names.csv') do |lookup|
      index_PL = lookup.index('foo')
      if index_PL
        output << lookup[2]
      end
    end
  end

ok, so, if I want to return all instances of foo, but in a csv, then how does that work?
so what I’d like as an outcome is happy, sad, hungry, bad. I thought it would be:

  FasterCSV.open('newfile.csv', 'w') do |output|
    FasterCSV.foreach('names.csv') do |lookup|
      index_PL = lookup.index('foo')
      if index_PL
        build_str << "," << lookup[2]
      end
      output << build_str
    end
  end

but it does not seem to work

  • 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-25T00:11:21+00:00Added an answer on May 25, 2026 at 12:11 am

    Replace foreach with open (to get an Enumerable) and find:

    FasterCSV.open('newfile.csv', 'w') do |output|
        output << FasterCSV.open('names.csv').find { |r| r.index('foo') }[2]
    end
    

    The index call will return nil if it doesn’t find anything; that means that the find will give you the first row that has 'foo' and you can pull out the column at index 2 from the result.

    If you’re not certain that names.csv will have what you’re looking for then a bit of error checking would be advisable:

    FasterCSV.open('newfile.csv', 'w') do |output|
        foos_row = FasterCSV.open('names.csv').find { |r| r.index('foo') }
        if(foos_row)
            output << foos_row[2]
        else
            # complain or something
        end
    end
    

    Or, if you want to silently ignore the lack of 'foo' and use an empty string instead, you could do something like this:

    FasterCSV.open('newfile.csv', 'w') do |output|
        output << (FasterCSV.open('names.csv').find { |r| r.index('foo') } || ['','',''])[2]
    end
    

    I’d probably go with the “complain if it isn’t found” version though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string

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.