aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaime <42359730+bracteatus@users.noreply.github.com>2019-07-27 00:07:11 -0600
committerJaime <42359730+bracteatus@users.noreply.github.com>2019-07-27 00:07:11 -0600
commit035b63413959f8a7218f0595751b14dd7d26a3b4 (patch)
tree63da0fabd6a226dc5403d75ec734f5881df7ddea
parent81caa1238297c1e7aba934168200c314265bd735 (diff)
downloadperlweeklychallenge-club-035b63413959f8a7218f0595751b14dd7d26a3b4.tar.gz
perlweeklychallenge-club-035b63413959f8a7218f0595751b14dd7d26a3b4.tar.bz2
perlweeklychallenge-club-035b63413959f8a7218f0595751b14dd7d26a3b4.zip
Update ch-1.pl
Iterate indices instead of substrings.
-rw-r--r--challenge-018/jaime/perl5/ch-1.pl12
1 files changed, 2 insertions, 10 deletions
diff --git a/challenge-018/jaime/perl5/ch-1.pl b/challenge-018/jaime/perl5/ch-1.pl
index ce9415e8c2..eeb61845eb 100644
--- a/challenge-018/jaime/perl5/ch-1.pl
+++ b/challenge-018/jaime/perl5/ch-1.pl
@@ -4,18 +4,10 @@
# Write a script that takes 2 or more strings as command line parameters
# and print the longest common substring.
-sub of_size {
- my ($n,$s) = @_;
- my @subs = ();
- for (my $i = 0; ($i+$n) <= length $s; $i++) {
- push @subs, substr($s,$i,$n);
- }
- return @subs; #substrings of $s of length $n.
-}
-
my $head = shift;
for my $n (reverse 1..(length $head)) {
- for my $s (of_size($n,$head)) {
+ for my $i (0..((length $head)-$n)) {
+ my $s = substr($head,$i,$n);
if (@ARGV == grep(/$s/,@ARGV)) {
print "$s\n";
exit;