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

Sean McKenna sean at mckennaprod.com
Wed Jul 24 11:21:31 PDT 2013


I think that the @str[1..$#str] also needs to be double quoted
otherwise you get a Use of uninitialized value in concatenation error.

At least in my version 5.12

#!/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";

This works for me

On Wed, Jul 24, 2013 at 10:21 AM, Randy J. Ray <rjray at blackperl.com> wrote:
> On 07/24/2013 10:12 AM, Richard Reina wrote:
>>
>> 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
>
>
> Oops, my bad. I didn't notice that you aren't really getting an array slice
> on the last line. That needs to be:
>
> print "Second thru last element: " . @str[1..$#str] . "\n";
>
> Note the "@" before str.
>
>
> Randy
> --
> """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
> Randy J. Ray      Sunnyvale, CA      http://www.rjray.org
> rjray at blackperl.com
>
> Silicon Valley Scale Modelers: http://www.svsm.org
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


More information about the SanFrancisco-pm mailing list