enumerate all adjecent substrings in the file

Discussion in 'Perl Misc' started by puzzlecracker, Dec 11, 2005.

  1. I have a file and I want to list all substirngs e.x.

    abs =[a ab abs bs ]
    puzzlecracker, Dec 11, 2005
    #1
    1. Advertising

  2. puzzlecracker

    Guest

    "puzzlecracker" <> wrote:
    > I have a file and I want to list all substirngs e.x.


    I hope it is a pretty small file.

    > abs =[a ab abs bs ]


    What about b and s?


    # 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,'';
    };

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Dec 11, 2005
    #2
    1. Advertising

  3. wrote:
    > "puzzlecracker" <> wrote:
    > > I have a file and I want to list all substirngs e.x.


    > # 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,'';
    > };


    For this particular requirement the nested loop approach is possibly
    the most efficient but there is also the approach of finding all
    matches for the pattern /.+/.

    sub all_matches {
    my ($string, $pattern) = @_;
    local our @strings; # Lexicals don't play nicely with (?{})
    my $capture = qr/(?{ push @strings => $1 })/;
    $string =~ /($pattern)$capture(?!)/;
    @strings;
    }

    print join ' ', all_matches('abs',qr/.+/);
    Brian McCauley, Dec 11, 2005
    #3
  4. puzzlecracker

    Anno Siegel Guest

    puzzlecracker <> wrote in comp.lang.perl.misc:
    > I have a file and I want to list all substirngs e.x.
    >
    > abs =[a ab abs bs ]


    That aren't all substrings.

    print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 11, 2005
    #4
  5. puzzlecracker

    Ron Savage Guest

    On Mon, 12 Dec 2005 01:21:31 +1100, -berlin.de wrote:

    Hi Anno

    > print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;


    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?
    Ron Savage, Dec 12, 2005
    #5
  6. puzzlecracker

    Anno Siegel Guest

    Ron Savage <> wrote in comp.lang.perl.misc:
    > On Mon, 12 Dec 2005 01:21:31 +1100, -berlin.de wrote:
    >
    > Hi Anno
    >
    > > print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;

    >
    > 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?


    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;
    }

    Scratch it.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 12, 2005
    #6
  7. Anno Siegel wrote:
    > Ron Savage <> wrote in comp.lang.perl.misc:
    >>On Mon, 12 Dec 2005 01:21:31 +1100, -berlin.de wrote:
    >>
    >>>print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;

    >>Neat, but you should have tested it:
    >>
    >>my($str) = 'abc';
    >>print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;
    >>
    >>produces
    >>
    >>a
    >>b
    >>c
    >>ab
    >>abc
    >>
    >>Where is bc?

    >
    > 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;
    > }


    Or just:

    print "$_\n" for map $str =~ /(?=(.{$_}))/g, 1 .. length $str;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Dec 12, 2005
    #7
  8. puzzlecracker

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > Ron Savage <> wrote in comp.lang.perl.misc:
    > >>On Mon, 12 Dec 2005 01:21:31 +1100, -berlin.de wrote:
    > >>
    > >>>print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;
    > >>Neat, but you should have tested it:
    > >>
    > >>my($str) = 'abc';
    > >>print "$_\n" for map $str =~ /.{$_}/g, 1 .. length $str;
    > >>
    > >>produces
    > >>
    > >>a
    > >>b
    > >>c
    > >>ab
    > >>abc
    > >>
    > >>Where is bc?

    > >
    > > 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;
    > > }

    >
    > Or just:
    >
    > print "$_\n" for map $str =~ /(?=(.{$_}))/g, 1 .. length $str;


    Ah, thanks... much better. Empty matches don't match twice at the same
    place, that's why it makes progress without ever matching anything
    substantial.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 12, 2005
    #8
  9. puzzlecracker

    Dr.Ruud Guest

    John W. Krahn:

    > print "$_\n" for map $str =~ /(?=(.{$_}))/g, 1 .. length $str;


    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;
    }


    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Dec 12, 2005
    #9
  10. puzzlecracker

    Anno Siegel Guest

    Dr.Ruud <> wrote in comp.lang.perl.misc:
    > John W. Krahn:
    >
    > > print "$_\n" for map $str =~ /(?=(.{$_}))/g, 1 .. length $str;

    >
    > 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;
    > }


    Ah, you're building the set of unique substrings instead of the list
    of all of them.

    The interesting part is that this solves a problem with "unique" in
    the specification without using a hash. A hash solution looks more
    complicated, at least when forced into a single statement:

    {
    local($,, $\) = ("\t", "\n");
    my $str = 'abcabc';

    print for keys %{
    {
    map { $_ => 1 }
    map $str =~ /(?=(.{$_}))/g, 1..length $str
    }
    };
    }

    Anno

    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Dec 13, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UHJhZGVlcCBTdW5kYXJhbShNU0ZUKQ==?=

    Enumerate all the drives including Mapped network drives on a serv

    =?Utf-8?B?UHJhZGVlcCBTdW5kYXJhbShNU0ZUKQ==?=, Feb 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,022
    =?Utf-8?B?UHJhZGVlcCBTdW5kYXJhbShNU0ZUKQ==?=
    Feb 26, 2005
  2. Replies:
    8
    Views:
    643
    Thorsten Kampe
    Jul 13, 2006
  3. Malcolm Dew-Jones
    Replies:
    1
    Views:
    368
    Joseph Kesselman
    Feb 27, 2008
  4. Robert Reno

    How to get all possible substrings

    Robert Reno, Jan 26, 2010, in forum: C++
    Replies:
    4
    Views:
    556
    P. Lepin
    Jan 28, 2010
  5. kadau
    Replies:
    3
    Views:
    95
    Brian McCauley
    Mar 25, 2005
Loading...

Share This Page