in reply to Radoteur
Hello QuillMeantTen,
If you see any improvements or corrections to my code, please do tell me so
Well, since you asked ... :-)
prints a newline character followed by 24 spaces between 0 and again\n. This is almost certainly not what you intended.printf STDERR "we have cycled: doing line $. le +tter 0 again\n";
Here’s my re-write of the code. In addition to the points noted above, I’ve streamlined the script’s control flow. Comments have been removed:
#! perl use strict; use warnings; use autodie; die "To use me, give me the word list file as argument\n" unless defined $ARGV[0]; my %letter_lines; my $evt; my $niter = 0; for (my $cycled = 0; !$cycled; warn "going back to the beginning\n" unless $cycled) { open my $fh, '<', $ARGV[0]; FILE_ITER: while (my $word = <$fh>) { my @letters = split '', $word; ++$niter; if (defined $evt) { WORD_ITER: for my $i (0 .. $#letters) { if ($evt eq $letters[$i]) { my $j = $i + 1; $evt = $i < $#letters ? $letters[$j] : undef; if (defined $evt) { if (defined $letter_lines{$.}{$j}) { warn "we have cycled: line $. letter $j\n" +; $cycled = 1; last FILE_ITER; } warn "adding to hash line $. letter $j\n"; $letter_lines{$.}{$j} = 1; print $evt; } last WORD_ITER; } } } elsif (defined $letter_lines{$.}{0}) { warn "we have cycled: doing line $. letter 0 again\n"; $cycled = 1; last FILE_ITER; } else { $letter_lines{$.}{0} = 1; warn "adding to hash line $. letter 0\n"; $evt = shift @letters; print $evt; } } close $fh; } warn "cycled in $niter iterations\n";
Tested with “linuxwords.txt” downloaded from https://users.cs.duke.edu/~ola/ap/linuxwords. With allowances made for changes to certain print statements (see point (2), above), the outputs to STDOUT and STDERR are identical for the original and revised scripts.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Radoteur
by QuillMeantTen (Friar) on Oct 19, 2015 at 06:51 UTC | |
by stevieb (Canon) on Oct 19, 2015 at 14:46 UTC | |
by QuillMeantTen (Friar) on Oct 20, 2015 at 08:57 UTC | |
by BrowserUk (Patriarch) on Oct 20, 2015 at 09:08 UTC | |
by QuillMeantTen (Friar) on Oct 20, 2015 at 12:18 UTC | |
|