http://www.perlmonks.org?node_id=998386


in reply to Help with regular expression

Hello, I have a similar soluttion mentioned here by simply substitutin all brackets with a whitespace then split everything by whitespaces.

#!/usr/bin/perl -w use strict; $ARGV[0] = "www.perlmonks.org.txt"; open FH, $ARGV[0] or die "Can`t open it: $!\n"; my @parsed = grep { s/[()]/ /igsx } <FH>; close FH; foreach (@parsed) { print $_, "\n"; } #some testing outputs my @vars=(); foreach (@parsed) { push @vars, split(" ", $_); } foreach (@vars) { print $_, "\n"; }

It just parsed them with the corresponding vars, but you may need more and more parsing for youur desires.

Tree structure will be a good solution for that task.

Replies are listed 'Best First'.
Re^2: Help with regular expression
by choroba (Cardinal) on Oct 11, 2012 at 08:19 UTC
    You lose the structure. Also, it does not work for values containig whitespace, as in HLD.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Point taken. I am still a learner and I try to catch up with you guys, I`ve just beat Lama book and I am going to Alpaca, so I didn`t know how to make C`s arrays of arrays or hashes with arrays, so building a real tree wasn`t possible for me yet. However one of the users offered a stack, something I did not come up with, and quite easy one in Perl... More practice I guess.