Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: strings with options - how do I do this?

by blakem (Monsignor)
on Feb 27, 2003 at 10:21 UTC ( [id://239048]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://239048]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found