aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorboblied <boblied@gmail.com>2021-02-09 13:11:40 -0600
committerboblied <boblied@gmail.com>2021-02-09 13:11:40 -0600
commit6ca9e732b9def2bf1a727f79a7c261f5ed265410 (patch)
treebe45cade3a98ec327f2acba0351c7c76a1b01e96
parenta14e8a2d21fb0d242c3bea7bf62a1d9467f55726 (diff)
downloadperlweeklychallenge-club-6ca9e732b9def2bf1a727f79a7c261f5ed265410.tar.gz
perlweeklychallenge-club-6ca9e732b9def2bf1a727f79a7c261f5ed265410.tar.bz2
perlweeklychallenge-club-6ca9e732b9def2bf1a727f79a7c261f5ed265410.zip
Solution for 098 #1, Read N Characters
-rwxr-xr-xchallenge-098/bob-lied/perl/ch-1.pl61
1 files changed, 61 insertions, 0 deletions
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);