Re: Iterating over Arrays

[email protected] (William Torrez Corea)
Newsgroups perl.beginners
Message-ID <CAGiORHf91VbR8J58koSBwQLyAzkthHbFcJdb+8Votgjyt5gPJQ@mail.gmail.com>
On Sun, Sep 25, 2022 at 12:02 PM Mike <[email protected]> wrote:

>
> 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.
> >
>
>

I have the following version:

v5.32.1

-- 

With kindest regards, William.

⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀⠀⠀⠀
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.