aboutsummaryrefslogtreecommitdiff
path: root/challenge-098
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-09 22:15:09 +0000
committerGitHub <noreply@github.com>2021-02-09 22:15:09 +0000
commit26ce3466d589df0ee7e5994089d80f73af096fd5 (patch)
tree566270cf6055e382368b7f82822a2f6edc2c65ab /challenge-098
parent27d58aa214b1a22f538582efbdf980142fe137b8 (diff)
parent6ca9e732b9def2bf1a727f79a7c261f5ed265410 (diff)
downloadperlweeklychallenge-club-26ce3466d589df0ee7e5994089d80f73af096fd5.tar.gz
perlweeklychallenge-club-26ce3466d589df0ee7e5994089d80f73af096fd5.tar.bz2
perlweeklychallenge-club-26ce3466d589df0ee7e5994089d80f73af096fd5.zip
Merge pull request #3495 from boblied/master
PWC 098 #1
Diffstat (limited to 'challenge-098')
-rw-r--r--challenge-098/bob-lied/README4
-rwxr-xr-xchallenge-098/bob-lied/perl/ch-1.pl61
2 files changed, 63 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/
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);