UP / HOME

Challenge 098

Table of Contents

Task 1 - Read N-characters

You are given file $FILE.

Create subroutine readN($FILE, $number) returns the first n-characters and moves the pointer to the (n+1)th character.

Raku

readN is defined as such:

sub readN (
    IO $file, Int $chars --> Str
) {
    ...
}

The pointer index is stored in a state array (%pointers). It's stores the pointer separately for each file. It's initialized with 0.

# %pointers stores the pointer index.
state Int %pointers;
%pointers{$file} = 0 without %pointers{$file};

The pointer is updated & required string is returned.

with %pointers{$file} -> $idx {
    %pointers{$file} += $chars;
    return $file.slurp.substr($idx, $chars);
}

Andinus / 2021-02-02 / Modified: 2021-10-11 Mon 19:56 Emacs 27.2 (Org mode 9.4.4)