Re: Iterating over Arrays
On Sun, Sep 25, 2022 at 12:02 PM Mike 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 ⠈⠳⣄
Re: Iterating over Arrays
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.
Re: Iterating over Arrays
Hello Try with this code: use strict; use warnings; use Data::Dumper; my @words = ("this","that"); # With this code $words[5] = "bad idea"; print Dumper \@words; And it outputs: $VAR1 = [ 'this', 'that', undef, undef, undef, 'bad idea' ]; so $words[2], $words[3], $words[4] have been defined as undef automatically. You'd better use a "push" method for a list operation like this. Thanks William Torrez Corea wrote: What happen with my code? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Iterating over Arrays
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. -- With kindest regards, William. ⢀⣴⠾⠻⢶⣦⠀ ⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system ⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org ⠈⠳⣄ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/