P
puzzlecracker
I have a file and I want to list all substirngs e.x.
abs =[a ab abs bs ]
abs =[a ab abs bs ]
puzzlecracker said:I have a file and I want to list all substirngs e.x.
abs =[a ab abs bs ]
# doesn't include the empty string.
# (If it did, how many times should it be include?)
while (length $string) {
foreach (1..length $string) {
print substr $string,0,$_;
};
substr $string,0,1,'';
};
puzzlecracker said:I have a file and I want to list all substirngs e.x.
abs =[a ab abs bs ]
print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;
Ron Savage said:On Mon, 12 Dec 2005 01:21:31 +1100, (e-mail address removed)-berlin.de wrote:
Hi Anno
Neat, but you should have tested it:
#!/usr/bin/perl
use strict;
use warnings;
# -----------
my($str) = 'abc';
print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;
produces
a
b
c
ab
abc
Where is bc?
Anno said:Oh, right. Overlapping substrings of each length are missing. Trying
to fix it gave me this:
for ( 0 .. length( $str) - 1 ) {
print "$1$2\n" while $str =~ /(.)(?=(.{$_}))/g;
}
John W. Krahn said:Or just:
print "$_\n" for map $str =~ /(?=(.{$_}))/g, 1 .. length $str;
print "$_\n" for map $str =~ /(?=(.{$_}))/g, 1 .. length $str;
Dr.Ruud said:John W. Krahn:
Variant:
#!/usr/bin/perl
use strict;
use warnings;
{
local($,, $\) = ("\t", "\n");
my $str = 'abcabc';
print "$_" for map $str =~ /(?=(.{$_}))/g, 1..length $str;
print '--';
print "$_" for map $str =~ /(?=(.{$_})(?!.*\1))/g, 1..length $str;
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.