Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Inserting an array in an array of hashes

by gibsonca (Beadle)
on Oct 18, 2012 at 17:17 UTC ( #999778=perlquestion: print w/ replies, xml ) Need Help??
gibsonca has asked for the wisdom of the Perl Monks concerning the following question:

What I am having problems with is that I want "price" to be modified to be an array of prices. So some of the zip files would have one price, while others may have two or more.

my @file_attachments = ( {file => 'test1.zip', price => '10.00', desc => 'the 1st test'}, {file => 'test2.zip', price => '12.00', desc => 'the 2nd test'}, {file => 'test3.zip', price => '13.00', desc => 'the 3rd test'}, {file => 'test4.zip', price => '14.00', desc => 'the 4th test'} ); # Get the number of items (hashes) in the array. my $file_no = scalar (@file_attachments); # $file_no is now: 4 in this instance as there is 4 hashes in the + array. # Looping through the hash and printing out all the hash "file" e +lements. for (my $i=0; $i < $file_no; $i++) { print '$file_attachments[$i]{'file'} is:'. $file_attachments[$i]{'fil +e'}."\n"; } # Looping through the hash and printing out all the hash "price" +elements. for (my $i=0; $i < $file_no; $i++) { print '$file_attachments[$i]{'price'} is:'. $file_attachments[$i]{'pri +ce'}."\n"; } # Looping through the hash and printing out all the hash "desc" e +lements. for (my $i=0; $i < $file_no; $i++) { print '$file_attachments[$i]{'desc'} is:'. $file_attachments[$i]{'desc +'}."\n"; }

Sample code came from here: http://htmlfixit.com/cgi-tutes/tutorial_Perl_Primer_013_Advanced_data_constructs_An_array_of_hashes.php

Comment on Inserting an array in an array of hashes
Download Code
Re: Inserting an array in an array of hashes
by toolic (Chancellor) on Oct 18, 2012 at 17:28 UTC
    perldsc
    use warnings; use strict; my @file_attachments = ( {file => 'test1.zip', price => ['10.00'], desc => 'the +1st test'}, {file => 'test2.zip', price => ['12.00', '12.50'], desc => 'the +2nd test'}, {file => 'test3.zip', price => ['13.00'], desc => 'the +3rd test'}, {file => 'test4.zip', price => ['14.00'], desc => 'the +4th test'} ); for (0 .. 3) { print "@{ $file_attachments[$_]{price} }\n"; } __END__ 10.00 12.00 12.50 13.00 14.00
Re: Inserting an array in an array of hashes
by choroba (Abbot) on Oct 18, 2012 at 17:28 UTC
    Documentation: Perl Data Structures Cookbook and Perl references and nested data structures.
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @file_attachments = ( {file => 'test1.zip', price => '10.00', des +c => 'the 1st test'}, {file => 'test2.zip', price => '12.00', des +c => 'the 2nd test'}, {file => 'test3.zip', price => '13.00', des +c => 'the 3rd test'}, {file => 'test4.zip', price => '14.00', des +c => 'the 4th test'} ); # Get the number of items (hashes) in the array. my $file_no = scalar (@file_attachments); # $file_no is now: 4 in this instance as there is 4 hashes in the arra +y. # Looping through the hash and printing out all the hash "file" elemen +ts. for my $file_attachment (@file_attachments) { print "$file_attachment\n"; } # Looping through the hash and printing out all the hash "price" eleme +nts. for my $file_attachment (@file_attachments) { print $file_attachment->{price}, "\n"; } # Looping through the hash and printing out all the hash "desc" elemen +ts. for my $file_attachment (@file_attachments) { print $file_attachment->{desc}, "\n"; } # Change price to an array reference. $file_attachments[0]{price} = [12, 12.5]; $file_attachments[1]{price} = [ $file_attachments[1]{price} ]; push @{ $file_attachments[1]{price} }, 10.5; $file_attachments[2]{price} = []; @{ $file_attachments[2]{price} } = (13, 13.5); print Dumper \@file_attachments;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Inserting an array in an array of hashes
by Kenosis (Priest) on Oct 18, 2012 at 17:29 UTC

    Were you looking for something like the following?

    use strict; use warnings; my @file_attachments = ( {file => 'test1.zip', price => ['10.00'], desc => 'the 1st test' +}, {file => 'test2.zip', price => ['12.00', '14.00'], desc => 'the +2nd test'}, {file => 'test3.zip', price => ['13.00', '15.00'], desc => 'the +3rd test'}, {file => 'test4.zip', price => ['14.00'], desc => 'the 4th test' +} ); for my $file_attachment (@file_attachments) { print "$file_attachment->{file}:\n"; print "$_\n" for @{ $file_attachment->{price} }; print "\n"; }

    Output:

    test1.zip: 10.00 test2.zip: 12.00 14.00 test3.zip: 13.00 15.00 test4.zip: 14.00
Re: Inserting an array in an array of hashes
by sundialsvc4 (Monsignor) on Oct 18, 2012 at 20:04 UTC

    Also bear in mind that the way in which Perl is actually doing this, explicitly or implicitly, is through the magic of references.   The value associated with a particular hash-key, like a particular element in an array, is always “a single scalar,” but ... a reference is “a single scalar,” no matter what it refers to.   That is the secret to creating arbitrary data-structures in Perl.   The memory-manager allows an arbitrary number of references to exist to the same piece of data and maintains a reference-count for each piece, automatically disposing of no-longer-referenced pieces.

    So, Perl’s technically-correct view of your title line would be:   “inserting a reference to an array, into one of the hashes which are referenced by:   an array, whose elements happen to entirely consist of:   references to hashes.”

    Self-evident:   hashref is slang for “reference to a hash;” arrayref is slang for “reference to an array.”

    Perl is a very forgiving language that encourages “say what I mean,” allowing you to say the same thing in several different ways ... but it is important to bear in mind that this is how all those ways of saying it wind up.   References are one of the “cornerstone ideas” of this language.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999778]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2014-09-18 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (114 votes), past polls