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

Replies are listed 'Best First'.
Re: Inserting an array in an array of hashes
by choroba (Bishop) 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 toolic (Bishop) 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 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 (Abbot) 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?
[Dumu]: got it. Anyway. Perl question. Can you use binmode more than once with an open filehandle to read a file first in binary mode, then in utf8 mode?
[ambrus]: Dumu: you can use binmode multiple times, but mostly at the start before you read things. it would be strange to change the io layers after you've already read things, and might not work too well.
[ambrus]: Dumu: why would you want to read the same file both in binary mode and utf8 mode?
[Corion]: Dumu: Yes, but switching needs to be done with caution, as you might read half an UTF-8 character in binary mode, and then switch to utf8 mode...
[Corion]: Personally, I would do the decoding of data in the program and not in the filehandle
[Corion]: ambrus: Maybe a record-based file where some data is strings encoded as utf8
[Corion]: But again, I would read all data as binary and then decode from utf8
[ambrus]: Corion: that wouldn't be my guess, but I'll wait for Dumu
[Corion]: Meh. I'm now on Firefox 57 ("Quantum") and again have to remove Pocket, and reinstall the adblocker, uMatrix (because NoScript doesn't work) and silence the about:blank page to not download crap from the internet
[Corion]: Maybe I shouldn't update software ...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2017-11-20 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (290 votes). Check out past polls.

    Notices?