Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to parse a scalar to see if it contains certain information that I am looking for. For example, each scalar may/may not contain the following information [x()]. In addition, the [x()] may appear more than once. Therefore, I need to loop through the scalar.

For example, the following scalar may exist:

$scalar = "anyfile_[w(one|two|three|four)].txt";

Whenever the [x()] appears, I need to be able to look at the information within the brackets. Depending on the value of "x", I need to perform a specific operation with the items that appear in the "()." As of right now, I am having a hard time with the loop and the regular expression. I really need some help with this.

How do I loop through the scalar and find the [x()] and extract the "x" and "()" information? What if there are more then one [x()]?

Any help and/or advice would be greatly appreciated.

JT

Replies are listed 'Best First'.
Re: Loop Through Scalar Obtain Information With Regular Expression
by dragonchild (Archbishop) on Jul 24, 2003 at 21:38 UTC
    Try using a regular expression to parse the information. You can capture values in a regular expression and use them in variables called $1, $2, etc. You can also use something like the following to loop through the matches.
    my $x = "abcccabcccabbbb"; while ($x =~ /([ab])/g) { print "Found '$1'\n"; }
    Run that code and you'll see what I mean.

    Using those two hints, you should able to complete your homew task.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Loop Through Scalar Obtain Information With Regular Expression
by sauoq (Abbot) on Jul 24, 2003 at 22:23 UTC

    Something like this?

    sub func_w { print "Called: func_w(@_)\n"; } sub func_x { print "Called: func_x(@_)\n"; } my %function = ( w => \&func_w, x => \&func_x, ); my $data = "blah[x(foo|bar|baz)] blah[w(boo|far|faz)]"; while ( $data =~ / \[ (.) \( ([^)]*) \) \]/gx ) { my $name = $1; my @args = split /\|/, $2; $function{ $name }->(@args); }

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Loop Through Scalar Obtain Information With Regular Expression
by Paladin (Vicar) on Jul 24, 2003 at 21:54 UTC
    If you do take dragonchild's advice, and look at regular expressions for this task, make sure you look at the Regexp::Common module. It has very useful features to make building complicated regular expressions easier, like matching balanced delimiters.
Re: Loop Through Scalar Obtain Information With Regular Expression
by Cody Pendant (Prior) on Jul 25, 2003 at 00:27 UTC
    It's not hard to grab expressions in brackets, as long as there's only one level -- as long as brackets never appear inside other brackets, or escaped or as part of quoted text.

    If, on the other hand, your expressions can ever look like

    [w([one]|[two]|[three]|[four])]
    or maybe
    [w(one bracket \)|two brackets \))]
    or
    [w(one of these ")"|one of these "]")]
    then we're in a whole other territory.

    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print