Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Associating values in Arrays

by Maresia (Beadle)
on Nov 10, 2015 at 16:39 UTC ( [id://1147374]=perlquestion: print w/replies, xml ) Need Help??

Maresia has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
Need to associate these values, I guess by the "ACCOUNT" value in each array, but I can’t figure it out the best way to do it.
#!/usr/bin/perl use strict; use warnings; use HTML::Template; use Data::Dump 'pp'; use Data::Dumper; my @princ; push @princ, ( {ACCOUNT=>'accountOne',NAME=>'name1',QDT=>'qdt1'}, {ACCOUNT=>'account2',NAME=>'name2',QDT=>'qdt2'}, {ACCOUNT=>'account3',NAME=>'name3',QDT=>'qdt3'}, ); my @type = (); push @type, ( {HOME_NUMBER=>'house for first account',YEAR=>'year for +forst account',MONTH=>'the month for the first account', ACCOUNT=>'ac +countOne',}, {HOME_NUMBER=>'house sec account',YEAR=>'year for the second +',MONTH=>'month for the second', ACCOUNT=>'account2',}, {HOME_NUMBER=>'house for the 3rd account', YEAR=>'year for 3 +',MONTH=>'month for the third', ACCOUNT=>'account3',}, ); my @person = (); push @person, ( {PERSON_ID=>'this is the id for the first account',PE +RSON=>'and the person for the first account',DATE=>'also the date for + the first account', ACCOUNT=>'accountOne',}, {PERSON_ID=>'id for the sec2',PERSON=>'This is the person2',DA +TE=>'The date forthe sec', ACCOUNT=>'account2',}, {PERSON_ID=>'The id3',PERSON=>'person data for the third acc', +DATE=>'The date matches the Third account', ACCOUNT=>'account3',}, ); my @data=(); my @n_type=(); my @n_person=(); my $n; for my $p (@princ){ ++$n; # In this block, I am stuck for my $i (1..$n){ push @n_type, \@type; push @n_person, \@person; }; push @data, { PRINC =>1,PRINC_DATA => [$p], TYPE =>1,TYPE_DATA => \@n_type, PERSON=>1,PERSON_DATA=> \@n_person }; } # Create and Load Template my $template_tmpl = HTML::Template->new( filehandle => \*DATA, die_on_ +bad_params => 1); $template_tmpl->param( DATA => \@data ); ...

Thanks for looking!

Replies are listed 'Best First'.
Re: Associating values in Arrays
by Skeeve (Parson) on Nov 11, 2015 at 09:45 UTC

    No idea what you define as "to associate".

    Anyhow: Why do you push all entries into an array? Why not use a hash?

    my %princ= ( accountOne => {NAME=>'name1',QDT=>'qdt1'}, account2 => {NAME=>'name2',QDT=>'qdt2'}, account3 => {NAME=>'name3',QDT=>'qdt3'}, );

    Do this for all your arrays and it shouldn't be a problem to access any entry by its account name.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found