Gauthier, Dave wrote: > Getting unwanted list elements when using split with regex. Here's an > example.... > > $str = "abc=In"; > @arr = split(/[a-zA-Z0-9]/,$str); > foreach $a (@arr) > {print "$a\n";} > > I get... > > <> > <> > <> > <=> > > If I change "abc=In" to "abcdef=In", I get 6 unwanetd null elements (one > per char before the "="). > I was expectiing a single element list with arr[0] = "=". > > What's Up ? Is ther a clen way to prevent creating these unwanted > elements?
In your example the string "abc=In" is being split using the expression /[a-zA-Z0-9]/. That expression matches in the string at positions 0, 1, 2, 4 and 5 therefore split will produce a list of *six* elements: $ perl -le' $str = "abc=In"; print ++$x, ": <$_>" for split /[a-zA-Z0-9]/, $str, -1; ' 1: <> 2: <> 3: <> 4: <=> 5: <> 6: <> Your example only shows four because split automatically discards any trailing empty elements. Even if you used a modifier /[a-zA-Z0-9]/+ with the expression you would still get the empty element at the beginning of the string. To get just the data you want without the empty elements you should use a global match instead: $ perl -le' $str = "abc=In"; print ++$x, ": <$_>" for $str =~ /[^a-zA-Z0-9]+/g; ' 1: <=> 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/