http://qs321.pair.com?node_id=239048


in reply to strings with options - how do I do this?

#!/usr/bin/perl -wT use strict; my $text = "my text [and maybe|and here's] more text [the end|stop]"; my @arr = parseit($text); print "$_\n" for @arr; sub parseit { my $text = shift; for ($text) { s/\[/{/g; s/\]/}/g; s/\|/,/g; } glob($text); } __END__ my text and maybe more text the end my text and maybe more text stop my text and here's more text the end my text and here's more text stop

-Blake

Replies are listed 'Best First'.
Re: Re: strings with options - how do I do this?
by Hofmator (Curate) on Feb 27, 2003 at 10:31 UTC
    Works fine with the caveat that the string shouldn't contain any meta characters that glob might want to expand, like '*'. And this:
    for ($text) { s/\[/{/g; s/\]/}/g; s/\|/,/g; }
    could be written as: $text =~ tr/[|]/{,}/;

    -- Hofmator

      Good points... How about this:
      use File::Glob qw(:glob); sub parseit { my $text = shift; $text =~ tr/[|]/{,}/; $text =~ s/([\\?*])/\\$1/g; map {s/\\([\\?*])/$1/g; $_} bsd_glob($text, GLOB_BRACE | GLOB_NOCHECK); }

      -Blake

        thank you people ... that does it perfectly. I learnt my "something new" for today too.
Re: Re: strings with options - how do I do this?
by danmcb (Monk) on Feb 27, 2003 at 10:27 UTC
    thank you Blake - that was damn fast! I'm going to check it out later. Daniel
Re^2: strings with options - how do I do this? (KGlob)
by tye (Sage) on Feb 27, 2003 at 18:22 UTC

    FYI, some Perl4 code for doing this can be found as part of File::KGlob as the unbrac() subroutine in KGlob.pm. That avoids conflicts with other "wildcard" characters.

                    - tye