Reverse the order of columns in a CSV file

Currently, I am importing a csv file into MaxMSP and running it through a javascript object. The process involves the following steps: 1) Removing the header line from the file 2) Transforming the csv file into an array 3) Converting elements from the array into floats (including dates) 4) Adding an index number in front of each line (as required by the coll object in MaxMSP) One issue I'm facing is that the data in the csv file is in reverse chronological order. I need to rearrange the data chronologically. It seems logical to read the file backwards line by line (after removing the header). You can find the link to the app and the csvfile here. Simply double-click on the js object to view the code. Once you drop a csv file on the hotspot, double-click on the coll object to see its contents.

Below is the Javascript code snippet:

    function import(filename)
{
    var f = new File(filename); 
    if (f.open) {
        var i = 0;
        outlet(0, "clear");
        f.readline();


        while (f.position < f.eof)
        {
            var str = f.readline(); 


            var a = str.split(","); // convert strings to array (elements are delimited by a comma)
            // a[5] /= 1000; // uncomment to divide the 6th column by 1000      


            var date = Date.parse(a[0]);
            var date = parseFloat(date);
            var open = parseFloat(a[1]);
            var high = parseFloat(a[2]);
            var low = parseFloat(a[3]);
            var close = parseFloat(a[4]);
            var volume = parseFloat(a[5]);
            var adjusted_close = parseFloat(a[6]);


      outlet(0, i++, date,open,high,low,close,volume,adjusted_close); // store in the coll        
        }
        f.close();
    } else {
        error("couldn't find the file ("+ filename +")\n");
    }
}

Answer №1

Looking for a solution? Try this approach:

function loadFile(fileURL)
{
    var file = new File(fileURL);
    var data = [];

    if (file.exists) {
        var line = file.readln(); //Skipping the first line
        while (!file.eof) {
            var line = file.readln(); 
            data.push(line);
        }
        file.close();
    } else {
        error("The file ("+ fileURL +") could not be found\n");
    }

    //Now you can manipulate each line in the data array as needed
    for (var j=(data.length-1); j>=0; j--) {
        console.log(data[j]);
        //Feel free to parse each line's contents from the CSV file now
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with JQuery time picker functionality not functioning properly upon repeat usage

I am facing an issue with a modal dialog that contains a form loaded via ajax. The form includes a time field populated using the jquery timepicker. Everything works perfectly when I open the dialog for the first time. However, if I try to load the dialog ...

using javascript to target a specific css selector attribute

I have a CSS file with the following code: .datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4; font-size: 16px ;font-weight: normal; } Is there a way to use JavaScript to dynamically change the font size? The code below worked ...

Having issues with EasySearch functionality; it was functioning correctly previously

Using Meteor has completely transformed my web development experience. Javascript has become incredibly fun for me. I recently integrated the EasySearch solution on my site, but unfortunately it seems to have stopped working. It was functioning perfectly ...

Unable to integrate bootstrap with webpack

Incorporating Express, Webpack, and Bootstrap. Currently utilizing css-loader for my stylesheets. Below is the contents of my webpack.config.js: const path = require("path"); config = { "entry": "./modules/entry.js", "output": { "path": p ...

Acquire the selected option's value and display it within a div container

Is there a way to extract and display only the price value (after the " - ") within a div with the id of "price", updating as the select element is changed? <select id="product-select" name=""> <option value="1">Product Name / Yellow / S - ...

Delightful Bootstrap Tabs with Dynamic Content via Ajax

My website has a lot of tabs designed with Bootstrap. I wanted to make them responsive, so I tried using a plugin called Bootstrap Tabcollapse from https://github.com/flatlogic/bootstrap-tabcollapse (you can see a demo here: http://tabcollapse.okendoken.co ...

What are the steps for implementing if-else statements in JavaScript when working with multiple dropdown lists?

I have encountered an issue while trying to display options in multiple drop-down lists. Only two words (CHENNAI AND IOS-XE, BANGALORE AND IOS-XR) are being displayed and the rest of the words are not appearing. Can someone provide assistance on fixing thi ...

Upcoming release of Nextjs version 13 will introduce a complete client-side rendering configuration

Does NextJS 13 offer a configuration option for exclusively using full client-side rendering, without default server-side rendering? ...

Conceal rows in a table that are not selected using Jquery when the table has been loaded from a given URL

Below is the HTML code for an user search input field and the results table: <div class="input-group"> <input type="text" id="q" class="form-control" placeholder="Search for User"> <span class="input-group-btn"> ...

Submit data using PHP and then forward the user to the next page

I am looking for a way to send data to a page using $_POST and redirection without relying on JavaScript or manually writing HTML forms. Is there a solution that involves CURL instead? Any guidance on how to achieve this would be greatly appreciated! Thank ...

Error: The property 'length' cannot be read from an undefined parent causing Uncaught TypeError

Hey there, take a look at this cool stuff http://jsfiddle.net/J9Tza/ <form class="validation"> <div> <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" r ...

Clearing LocalStorage and Cache upon initial loading in Next.js

Our project requires continuous updates, and currently users are required to manually clear their cache and localStorage data after each update in order to access the new features. Is there a solution to automatically clear all cached data upon user visi ...

Errors in production arise from building React applications

I am new to using React and recently built a ToDo web app following a tutorial. Everything seemed fine during development, but when I tried to view the production version locally using serve -s build, two errors popped up that were not present before. reac ...

How can I retrieve the PHP response once a successful upload has occurred using DropzoneJS?

I am currently in the process of integrating Dropzone into my website. My goal is to capture the "success" event and extract specific information from the server response to add to a form on the same page as the DropZone once the upload is finished. The k ...

Tips for setting up Craigslist email forwarding through Node.js (receiving emails to a dynamically generated email address and redirecting them)

After extensive searching, I only came across this Stack Overflow post about Email Forwarding like Craigslist in Rails: Email Forwarding like Craigslist - Rails I spent a significant amount of time googling, but couldn't find any solutions using Node ...

What is the best way to extract data from a JSON file using Java?

I am working with a JavaScript file that contains the following object: var MoceanSettings={ BannerURL:'', IsDirectWall:true, AppOfDayZone:156431, ImpressTrackUrl:null, ClickTrackUrl:null, Categories:[ ...

Sorting the array in MongoDB before slicing it

Currently, I have a pipeline that aggregates Regions along with their respective countries and sales values. My goal is to obtain the top 5 countries by sales in each region using the $slice method. However, the issue I am facing is that it returns the fir ...

Ways to eliminate all attributes and their corresponding values within HTML tags

Hey there, I'm trying to strip away all the attribute values and styles from a tag in html Here's my Input: <div id="content"> <span id="span" data-span="a" aria-describedby="span">span</span> <p class="a b c" style=" ...

Using the mapState function in vuex allows for easy access to Vue methods

I have an important task to complete while the data is being computed using vuex mapState. I must ensure that the vue method countAlerts is called every time the data changes; however, the computed property needs to invoke this method and unfortunately, ...

Achieving a bottom tab bar display exclusively on default screens within nested stacks in react-navigation

Suppose I have a Tab Navigator where each tab contains nested stack navigators (refer to the code snippet below). My goal is to have the tab bar visible only on the default screen of each nested stack, similar to how the current Twitter Android app operate ...