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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:05:05+00:00 2026-06-06T02:05:05+00:00

I need to read a file line by line, and change a variable accordingly.

  • 0

I need to read a file line by line, and change a variable accordingly.
I would normally write this in PHP… but I decided to take the challenge.

I wrote:

fs = require('fs');
Lazy = require('lazy');
path = require('path');

files = fs.readdirSync('.');
var software = {};


files.forEach( function(fileName){


  var m; 
  if( m = fileName.match(/^(.*)\.txt$/) ){
    name = m[1];

    console.log("Processing file: " + fileName);
    software[name] = {};
    console.log("Software 1: %j",software);

    var section = 'unset';
    new Lazy(fs.createReadStream(fileName)).lines.forEach(
      function(line){
        var m;
        line = line + '';
        if( m = line.match(/^([a-zA-Z_]*):$/)){
          section = m[1];
          software[name][section] = '';
          console.log("Switching to section " + m[1]);
          console.log("Software 2: %j",software);
        } else if (line == '.'){
          section = 'unset'
        } else if (line == ''){
          section = 'unset'
        } else { 
          console.log("LINE: " + line) ;
          software[name][section] = software[name][section] + line + "\n";
          console.log("Software 3: %j",software);
        }
      }

    );   
  }

});

console.log("Software 4: %j",software);

Apart from the code being very ugly and very unoptimised, I am having trouble as when the last line prints, the “software” variable is not YET populated! I am guessing Lazy is asyncronous. So, it basically works, but “at some point later”. This is great, but… where do I write code when that important cycle, that fills in the software variable, is actually finished?!?

As requested: data to play with!

simply create “something.txt” and write:

name:
Name 1
.

Option 1:
Value 1
.

Option 2:
Value 2
.

Option 3:
Multi
Line
Value
.

Another_section:
Again
.

Merc.

  • 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-06-06T02:05:07+00:00Added an answer on June 6, 2026 at 2:05 am

    The instances of Lazy returned by the library are EventEmitters, and it emits en event called pipe when a “set” of operations is complete:

    new Lazy(
      ...
    ).on('pipe', function() {
      // all done
    });
    

    Modifying your code to use this event results in (the only change is near the bottom):

    fs = require('fs');
    Lazy = require('lazy');
    path = require('path');
    
    files = fs.readdirSync('.');
    var software = {};
    
    
    files.forEach( function(fileName){
    
    
      var m;
      if( m = fileName.match(/^(.*)\.txt$/) ){
        name = m[1];
    
        console.log("Processing file: " + fileName);
        software[name] = {};
        console.log("Software 1: %j",software);
    
        var section = 'unset';
        new Lazy(fs.createReadStream(fileName)).lines.forEach(
          function(line){
            var m;
            line = line + '';
            if( m = line.match(/^([a-zA-Z_]*):$/)){
              section = m[1];
              software[name][section] = '';
              console.log("Switching to section " + m[1]);
              console.log("Software 2: %j",software);
            } else if (line == '.'){
              section = 'unset'
            } else if (line == ''){
              section = 'unset'
            } else {
              console.log("LINE: " + line) ;
              software[name][section] = software[name][section] + line + "\n";
              console.log("Software 3: %j",software);
            }
          }
    
        ).on('pipe', function() {
          console.log("Software 4: %j",software);
        });
      }
    
    });
    

    [Edit] To answer your question regarding how I found this info:

    I did indeed check out the source file for the project; I knew the library had a sum method that could be chained to instances of Lazy to sum up everything at the end; the code for that method calls foldr, and the code for that method listens for an event called pipeName, which is defaulted in line 22 as pipe.

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

Sidebar

Related Questions

I need to read a file in PHP, but I only know the end
As I need to read a file when my app is launched and write
I have a file and I need to read it line after line. Each
I need to read a file in MB chunks, is there a cleaner way
I need to read a file from the local file system using JSP, save
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I need to read a PNG file and interpret all the information stored in
I need to read a whole file into memory and place it in a
My machine is little-endian (Intel byte order). I need to read a binary file
I need to read 16 bits from the binary file as std::string or char

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.