Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

strings with options - how do I do this?

by danmcb (Monk)
on Feb 27, 2003 at 10:13 UTC ( #239045=perlquestion: print w/replies, xml ) Need Help??
danmcb has asked for the wisdom of the Perl Monks concerning the following question:

This is a problem for a routine(s) I'm thinking of writing.
Given a piece of text like this:
"my text [and maybe|and here's] more text"
I want an array (or other collection) of these strings
"my text and maybe more text" "my text and here's more text"
given this line:
"my text [and maybe|and here's] more text [the end|stop]"
I would get:
"my text and maybe more text the end" "my text and here's more text the end" "my text and maybe more text stop" "my text and here's more text stop"
and given:
"my text [[and|or] maybe|and here's] more text"
I want:
"my text and maybe more text" "my text or maybe more text" "my text and here's more text"
(all characters except | should be just treated like text)
Something like this must exist - anyone know where?
thanks
Daniel

Replies are listed 'Best First'.
Re: strings with options - how do I do this?
by blakem (Monsignor) on Feb 27, 2003 at 10:21 UTC
    #!/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

      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 Blake - that was damn fast! I'm going to check it out later. Daniel

      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
Node Status?
node history
Node Type: perlquestion [id://239045]
Approved by Hofmator
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2018-07-17 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (355 votes). Check out past polls.

    Notices?