dylanthomasfan wrote:
Hi All,

Hello,

I already know of a way to do the following, but I want to know the
simplest way to do this in perl:

I have an input string which is of the following form:

((a+b)*c)*(e+f*(g+h-i*(j+k)))+u

I want to know how to split it easily so that I end up with an array
@tokens, which has the following contents:
['(', '(', 'a', '+', 'b', ')', '*', 'c', ')', '*', '(', 'e', '+', 'f',
'*', '(', 'g', '+', 'h', '-', 'i', '*', '(', 'j', '+', 'k', ')', ')',
')', '+', 'u']

I tried the following:

my @tokenSplit=split("[(\+),(\*),(\(),(\))]", $ARGV[0]);

You are using a character class comprised of the characters '(', '+', ')', ',' and '*' which could be written more simply as:

my @tokenSplit = split /[(),+*]/, $ARGV[ 0 ];


That didn't work.

Probably because split() removes the patterns used in the first argument but you appear to want to keep them.


where each of the alphabetical letters above can be complex strings,
but only [a-zA-Z0-9]*. For example, the following string is also
acceptable input:

((numberOfApples*priceOfApples)+(numberOfOranges - (numberOfBananas -
numberOfPlaintains)*17))

Try this:

my @tokenSplit = grep length, split /\s*([[:punct:]])\s*/, $ARGV[ 0 ];



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to