How to resolve the algorithm Remove lines from a file step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Remove lines from a file step by step in the zkl programming language

Table of Contents

Problem Statement

Remove a specific line or a number of lines from a file. This should be implemented as a routine that takes three parameters (filename, starting line, and the number of lines to be removed). For the purpose of this task, line numbers and the number of lines start at one, so to remove the first two lines from the file foobar.txt, the parameters should be: foobar.txt, 1, 2 Empty lines are considered and should still be counted, and if the specified line is empty, it should still be removed. An appropriate message should appear if an attempt is made to remove lines beyond the end of the file.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Remove lines from a file step by step in the zkl programming language

Source code in the zkl programming language

fcn deleteLinesM(fname, start,num){
    blob:=File(fname).read();     // file to memory
    n:=blob.seek(Void,start-1);   // seek to line and remember it
    blob.del(n,blob.seek(Void,num)-n);
 
    File.stdout.write(blob);
}
deleteLinesM("nn.zkl", 2,5);

fcn deleteLinesL(fname, start,num){
    if (start < 1) throw(Exception.ValueError);
    f:=File(fname);
    do(start-1) { File.stdout.write(f.readln()) }
    do(num)     { f.readln(); }
    f.pump(File.stdout.write);
}
deleteLinesL("nn.zkl", 2,5);

  

You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the J programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the Brainf*** programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Memory layout of a data structure step by step in the OCaml programming language
You may also check:How to resolve the algorithm List comprehensions step by step in the Python programming language