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
Comments
In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation. comments powered by Disqus
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.