YuckFoo has asked for the wisdom of the Perl Monks concerning the following question:
I'm looking for a better (cleaner) way to do this. I'd like to get a list of unique substrings formed by removing one character from a string.
aabcc ==> abcc, aacc, aabc
I can't believe I had to use two temporary lists and a hash to do this. Golfers are welcome.
YuckFoo
#!/usr/bin/perl use strict; my $list = sslist('aabcc'); for (@{$list}) { print "$_\n"; } sub sslist { my ($str) = @_; my (@list, %uniq); my @chars = split('', $str); for my $i (0..@chars-1) { my @temp = @chars; splice(@temp, $i, 1); my $key = join('', @temp); if (!$uniq{$key}++) { push (@list, $key); } } return \@list; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: List of substrings
by kvale (Monsignor) on Jun 24, 2002 at 18:14 UTC | |
by jsprat (Curate) on Jun 24, 2002 at 18:41 UTC | |
by kvale (Monsignor) on Jun 24, 2002 at 18:49 UTC | |
|
Re: List of substrings
by japhy (Canon) on Jun 24, 2002 at 19:01 UTC |