From a14e8a2d21fb0d242c3bea7bf62a1d9467f55726 Mon Sep 17 00:00:00 2001 From: boblied Date: Tue, 2 Feb 2021 13:04:33 -0600 Subject: Update for 98 --- challenge-098/bob-lied/README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-098/bob-lied/README b/challenge-098/bob-lied/README index be9398e9a3..07a4221a5d 100644 --- a/challenge-098/bob-lied/README +++ b/challenge-098/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 83 by Bob Lied. +Solutions to weekly challenge 98 by Bob Lied. -https://perlweeklychallenge.org/blog/perl-weekly-challenge-083/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-098/ -- cgit From 6ca9e732b9def2bf1a727f79a7c261f5ed265410 Mon Sep 17 00:00:00 2001 From: boblied Date: Tue, 9 Feb 2021 13:11:40 -0600 Subject: Solution for 098 #1, Read N Characters --- challenge-098/bob-lied/perl/ch-1.pl | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 challenge-098/bob-lied/perl/ch-1.pl diff --git a/challenge-098/bob-lied/perl/ch-1.pl b/challenge-098/bob-lied/perl/ch-1.pl new file mode 100755 index 0000000000..59dfafcfe8 --- /dev/null +++ b/challenge-098/bob-lied/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge 098 Challenge 1 +# 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. +# Example: +# Input: Suppose the file (input.txt) contains "1234567890" +# Output: +# print readN("input.txt", 4); # returns "1234" +# print readN("input.txt", 4); # returns "5678" +# print readN("input.txt", 4); # returns "90" +#============================================================================= + +use strict; +use warnings; +use v5.20; + +use experimental qw/signatures /; + +sub Usage { "Usage: $0 filename N" }; + +my $FILE = shift; +my $N = shift; + +die Usage() unless $FILE; +die Usage() unless $N; +die "Need positive N" if ( $N <= 0 ); + +# Cache open file handles per file. The file handle will +# keep track of the position in the file, advancing each time +# that we call read(). +my %fileToFH; + +sub readN($s, $n) +{ + my $fh; + if ( exists $fileToFH{$s} ) + { + $fh = $fileToFH{$s}; + } + elsif ( open($fh, "<:utf8", $s) ) + { + $fileToFH{$s} = $fh; + } + else + { + die "Invalid filename $s ($!)"; + } + my $howmany = read($fh, my $bytes, $n); + return $bytes; +} +binmode(STDOUT, "utf8"); +say readN($FILE, $N); +say readN($FILE, $N); +say readN($FILE, $N); -- cgit