dylanthomasfan wrote:
> Hi All,
> 
> 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]);
> 
> That didn't work.
> 
> 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))

The program below seems to do what you require.

HTH,

Rob



use strict;
use warnings;

my $string = '((numberOfApples*priceOfApples)+(numberOfOranges -
(numberOfBananas - numberOfPlaintains)*17))';

my @tokens = $string =~ /\w+|\S/g;

print "$_\n" foreach @tokens;

**OUTPUT**

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


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


Reply via email to