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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:26:47+00:00 2026-05-26T22:26:47+00:00

I have this code which extracts data from a csv file and then reformats

  • 0

I have this code which extracts data from a csv file and then reformats it so that it can be compared with another data set:

def dataExtract
  dates = File.open(@filename_data).read.scan /\d{2}\/\d{2}\/\d{2}/
  data_extracted = []
  index = 0
  dates.each do |date|
    inbound_row = @data[4+(11*index)]
    outbound_row = @data[6+(11*index)]
    data_extracted.push [date, '4001', (inbound_row[1].gsub(/\,/,"").to_i + inbound_row[2].gsub(/\,/,"").to_i).to_s, 'AI', 'INBOUND']
    data_extracted.push [date, '4090', inbound_row[3].gsub(/\,/,""), 'AI', 'INBOUND']
    data_extracted.push [date, '1139', inbound_row[4].gsub(/\,/,""), 'RU STANDRD', 'INBOUND']
    data_extracted.push [date, '1158', inbound_row[5].gsub(/\,/,""), 'RU STANDRD', 'INBOUND']
    data_extracted.push [date, '4055', outbound_row[1].gsub(/\,/,""), 'RU PLUS', 'OUTBOUND']
    data_extracted.push [date, '4055', outbound_row[2].gsub(/\,/,""), 'AR', 'OUTBOUND']
    data_extracted.push [date, '1139', outbound_row[4].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
    data_extracted.push [date, '1158', outbound_row[5].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
    data_extracted.push [date, '4091', outbound_row[3].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
    index += 1
  end
  return data_extracted   
end

And here is a sample of the csv data (this is for one day. For multiple days there are chunks like this with an empty space inbetween them):

Date,BLOCK,,Wood,Miscellaneous,,Totals,MO
Monday,4055-RU,4055-AR,4091,1139,1158,,100
11/4/15,C Sort,B,C,iGPS,PECO,,
Starting,714,228,858,82,129,"2,011",
Sorted,"2,738",190,"1,110",144,228,"4,410",
Subtotal 1,"3,452",418,"1,968",226,357,"6,421",
Shipped,"2,700",0,"1,865",0,0,"4,565",
,752,418,103,226,357,"1,856",
Physical,752,418,103,226,357,"1,856",
Variance,0,0,0,0,0,0, 

The only data being used in this csv file (besides the dates) are the sorted and shipped rows. Anyways, like I said, this works, it just isn’t very pretty. Is there a better way to execute the dates.each block since there is repeated info (date is in each array + inbound/outbound)?

  • 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-26T22:26:47+00:00Added an answer on May 26, 2026 at 10:26 pm

    Maybe simpler, maybe not – but hopefully enough to give you an idea on how to continue to improve it.

    data_extracted = []
    
    inbound_data_set = [['4090',3,'AI'],['1139',4,'RU STANDRD'],['1158',5,'RU STANDRD']]
    outbound_data_set = [['4055',1,'RU PLUS'],['4055',2,'AR'],['1139',4,'RU STANDRD'],['1158',5,'RU STANDRD'],['4091',3,'RU STANDRD']]
    
    dates.each_with_index do |date,index|
      inbound_row = @data[4+(11*index)]
      outbound_row = @data[6+(11*index)]
      # this one stays as it's much different
      data_extracted << [date, '4001', (inbound_row[1].gsub(/\,/,"").to_i + inbound_row[2].gsub(/\,/,"").to_i).to_s, 'AI', 'INBOUND']
      inbound_data_set.each do |num,idx,type|
        data_extracted << [date, num, (inbound_row[idx].gsub(/\,/,"")), type, 'INBOUND']
      end
      outbound_data_set.each do |num,idx,type|
        data_extracted << [date, num, (outbound_row[idx].gsub(/\,/,"")), type, 'OUTBOUND']
      end
    end
    

    EDIT: the code was missing a ‘)’ on (inbound_row[idx].gsub(/\,/,””)) and (outbound_row[idx].gsub(/\,/,””)).

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

Sidebar

Related Questions

I have this code which will include template.php file from inside each of these
I have this code (which is way simplified from the real code): public interface
I have an application that extracts data from some xml that is stored in
I have put together a script which will upload a CSV file and then
I have an ETL application which extracts data from a source database (for which
I have this HTML from which I have to extract data: <html> <head></head> <body>
I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code which compiles and works as expected: class Right {}; class
I have this code which should create a splash image with either no animation

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.