How to change an element of an array in Perl
$i = 3; $array[$i] = $new_value;
Full example
examples/replace_element_of_array.pl
use strict; use warnings; use Data::Dumper; my @authors = ('Issac Asimov', 'Arthor C. Clarke', 'Ray Bradbury', 'Foo Bar', 'Philip K. Dick', 'H. G. Wells', 'Frank Herbert'); print Dumper \@authors; $authors[3] = 'Jules Verne'; print Dumper \@authors;
Before:
$VAR1 = [ 'Issac Asimov', 'Arthor C. Clarke', 'Ray Bradbury', 'Foo Bar', 'Philip K. Dick', 'H. G. Wells', 'Frank Herbert' ];
After:
$VAR1 = [ 'Issac Asimov', 'Arthor C. Clarke', 'Ray Bradbury', 'Jules Verne', 'Philip K. Dick', 'H. G. Wells', 'Frank Herbert' ];
Read more about arrays in Perl.
Published on 2020-11-02
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post