in reply to List of substrings

This one uses only a temp scalar and a hash:
use strict; my $string = shift; my %seen; map { my $temp = $string; substr $temp, $_, 1, ""; print "$temp\n" unless $seen{$temp}++; } (0..(length $string) - 1);
-Mark

Replies are listed 'Best First'.
Re: Re: List of substrings
by jsprat (Curate) on Jun 24, 2002 at 18:41 UTC
    I was about to post a similar solution, but you beat me to it ;-P

    The only comment I would make is use foreach instead of map. In void context map will build a list to return, which just gets thrown away.

      That's a good point. I was going to use the map to build a substring array, but changed my strategy halfway through and didn't clean up. Here is a corrected version:
      use strict; my $string = shift; my %seen; foreach (0..(length $string) - 1) { my $temp = $string; substr $temp, $_, 1, ""; print "$temp\n" unless $seen{$temp}++; }
      Thanks!

      -Mark