%data = (
'digits' => [1, 2, 3],
'letters' => ['a', 'b', 'c']
);
How can I push '4' into $data{'digits'}?I am new to Perl. Those $, @, % symbols look weird to me; I come from a PHP background.
4 Réponses :
# perl -de 0
DB<1> @a=(1,2,3)
DB<2> $name="a"
DB<3> push @{$name}, 4
DB<4> p @a
1234
Une référence non dure est une référence symbolique.
push @{data{'digits'}}, 4;
The @{} makes an array from a reference (data{'digits'} returns an array reference.) Then we use the array we got to push the value 4 onto the array in the hash.This link helps explain it a little bit.I use this link for any questions about hashes in Perl.
push @{ $data{digits} }, 4;
The official Perl documentation website has a good tutorial on data structures: perldsc, particularly the Hashes-of-Arrays section.$, @ and % are known as sigils.
Pour une option exotique mais très agréable à l'oeil, jetez un coup d'œil au autobox :: core code> module CPAN.
use autobox::Core;
my %data = (
digits => [1, 2, 3],
letters => ['a', 'b', 'c'],
);
$data{digits}->push(4);
$data{digits}->say; # => 1 2 3 4
Voir Perldoc Perldata et Perldoc Perldsc pour des informations sur les structures de données PERL.