[sf-perl] Anyone know why won't this work?

Richard Reina gatorreina at gmail.com
Wed Jul 24 10:12:59 PDT 2013


Sorry I'm still confused.  I get the same thing when I put string in
double quotes.

#!/usr/bin/perl -w

my $string = "The good\n\nThe bad\n\nThe Ugly";

my @str = split /\n\n/, $string;

print "Second thru last element: " . $str[1..$#str] . "\n";

OUTPUT:

perl test_split.pl
Use of uninitialized value within @str in range (or flip) at
test_split.pl line 7.
Argument "" isn't numeric in array element at test_split.pl line 7.
Second thru last element: The good

2013/7/24, Randy J. Ray <rjray at blackperl.com>:
> On 07/24/2013 09:42 AM, Richard Reina wrote:
>> Can anyone tell me what I'm doing wrong?
>>
>> #!/usr/bin/perl -w
>>
>> my $string = 'The good\n\nThe bad\n\nThe Ugly';
>>
>> my @str = split /\n\n/, $string;
>>
>> print "Second thru last element: " . $str[1..$#str] . "\n";
>>
>>
>> GIVES ME:
>>
>> Use of uninitialized value within @str in range (or flip) at
>> test_split.pl line 7.
>> Argument "" isn't numeric in array element at test_split.pl line 7.
>> Second thru last element: The good\n\nThe bad\n\nThe Ugly
>
> You need to put the declaration of $string in double-quotes. Right now,
> you are creating a string with literal "\" + "n" sequences in it, not
> newlines. And when you are trying to split, the regexp is looking for
> newlines. As a result, @str only has one element, the whole of $string.
> Thus, $str[1] is undefined (and $#str is 0, making your range go down
> instead of up, which yields a non-numeric value for the index, which
> causes the warning).
>
> Randy
> --
> """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
> Randy J. Ray      Sunnyvale, CA      http://www.rjray.org
> rjray at blackperl.com
>
> Silicon Valley Scale Modelers: http://www.svsm.org
>


More information about the SanFrancisco-pm mailing list