Shawn H Corey <shawnhco...@gmail.com> writes:

> Harry Putnam wrote:
>> Why is it that the first two splits do not produce any elements?
>>
>> #!/usr/local/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my $var = 100421;
>> my @elems1 = split(/\d/,$var);
>> my @elems2 = split(/./,$var);
>> my @elems3 = split(//,$var);
>
> split discards the pattern split on.  To keep it, place it in parentheses.
>
> my @elems = split( /(\d)/, $var );

[...]

Oh GACK... I knew that too.. but lost my wits I guess.

I thought I understood you but when I run a modified script (at the
end) I see empty elements where I don't expect them.

The actual thing I'm working up to was splitting this input var

`ev  100421 4'

In reality I want to ditch `ev ' work with 100421 <for that part> 
and finally use ` 4' for a quite different purpose.  So in the end
I'll use all but 'ev '.

In these posts I've just been experimenting with various ways to split
that.

The modified script below is still just concerned with 100421:

However, it is doing something with null strings or something that
wasn't expected.  Using LIMIT in that situation seem not to do
anything different.

#!/usr/local/bin/perl

use strict;
use warnings;

my $var = 100421;
my @elems1 = split(/(\d\d)/,$var); #, -1);
my @elems2 = split(/(..)/,$var);
my @elems3 = split(//,$var);

if (@elems1){
  print "elems1 ( \`split(/(\\d\\d)/,$var)' ):
has these elements:\n";
  for(@elems1){
    print "I\'m an element -> <$_>\n";
  }
  print"-------       -------       ---=---       -------      -------\n";
}
if (@elems2){
  print "elems2 ( \`split(/(..)/,$var)' ):
has these elements:\n";
  for(@elems2){
    print "I\'m an element -> <$_>\n";
  }
  print"-------       -------       ---=---       -------      -------\n";
}
if (@elems3){
  print "elems3 ( \`split(//,$var)' ):
has these elements\n";
  for(@elems3){
    print "I\'m an element -> <$_>\n";
  }
  print"-------       -------       ---=---       -------      -------\n";
}
-------        ---------       ---=---       ---------      -------- 
-------        ---------       ---=---       ---------      -------- 
output:

elems1 ( `split(/(\d\d)/,100421)' ):
has these elements:
I'm an element -> <>
I'm an element -> <10>
I'm an element -> <>
I'm an element -> <04>
I'm an element -> <>
I'm an element -> <21>
-------       -------       ---=---       -------      -------
elems2 ( `split(/(..)/,100421)' ):
has these elements:
I'm an element -> <>
I'm an element -> <10>
I'm an element -> <>
I'm an element -> <04>
I'm an element -> <>
I'm an element -> <21>
-------       -------       ---=---       -------      -------
elems3 ( `split(//,100421)' ):
has these elements
I'm an element -> <1>
I'm an element -> <0>
I'm an element -> <0>
I'm an element -> <4>
I'm an element -> <2>
I'm an element -> <1>
-------       -------       ---=---       -------      -------


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to