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

Re: Iterating over Arrays

Thread Previous
From:
Ken Peng
Date:
September 25, 2022 01:10
Subject:
Re: Iterating over Arrays
Message ID:
768b9e2e-28c3-f191-6c81-084cb10354a2@geekml.org
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?

Thread Previous


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