Re: Iterating over Arrays

[email protected] (Mike)
Newsgroups perl.beginners
Message-ID <[email protected]>
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.
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.