develooper Front page | perl.beginners | Postings from September 2022

Re: Iterating over Arrays

Thread Previous | Thread Next
From:
Mike
Date:
September 25, 2022 18:02
Subject:
Re: Iterating over Arrays
Message ID:
3c65dece-77be-40e9-a72c-010c8f2a3956@mflan.com

Your version produces this output in Perl 5.30.0:

this
that
Use of uninitialized value $element in concatenation (.) or string at 
trash.pl line 14.

Use of uninitialized value $element in concatenation (.) or string at 
trash.pl line 14.

Use of uninitialized value $element in concatenation (.) or string at 
trash.pl line 14.

bad idea

Which is what I would expect.



Another version:

use strict;
use warnings;

my @words = ("this","that");

$words[5] = "bad idea";

for my $element (@words){
     if (defined $element) {
         print "$element\n";
     }
     else {
         print "undefined\n";
     }
}

print "\a";


__END__



Output:

this
that
undefined
undefined
undefined
bad idea


Mike


On 9/24/22 20:00, William Torrez Corea wrote:
> What happen with my code?
>
> use strict;
> use warnings;
> use diagnostics;
>
> my @words = ("this","that");
>
> # With this code
> $words[5] = "bad idea";
>
> for my $element (@words){
>          print "$element\n";
> }
>
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> ~
> "words.pl" 13L, 167B                           1,1           All
>
>
> Use of uninitialized value $element in concatenation (.) or string at
> words.pl line 11.
>   at words.pl line 11.
>


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About