Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array or a Hash???

Status
Not open for further replies.

NashTrump

Technical User
Jul 23, 2005
38
0
0
GB
Hi,

Im gathering a selection of pieces of information and i want to look at each 'selection' in turn...

for example:

Im gathering sets like below:

Url
NameA
AmountA
NameB
AmountB
NameB
AmountB

I could possibly gather up to 5 or 6 different sets.
In each set i want to compare amounts from differnt sets to see which combination adds up to 10, if they do i want to retain the information about which amount comes from which set.

See the example below:

Paul
5
Mark
8
Luke
2

Martin
6
Jack
3
Frank
9

Matt
7
Graham
2
John
2

An example answer would be:
Paul
5
Jack
3
John
2

Ive tried code but i cant sort out the arrays. The code i have doesnt do much.
I am looping through extractions from a database and adding them to an array using the push function.

I noticed that they dont stay in the array in the same order i put them in, this leaves me knackered!!

Although when i am using the push function i am giving each a name:

push(@myArray,
Url => NameA => Paul
AmountA => 5
NameB => mark
AmountB => 8
NameB => Luke
AmountB =>2
)

I would like if possible to reference them like

$myArray[0] or something though im not sure if...o read this much text!!!) Kind regards Nash
 
is this real code or psuedo code?

push(@myArray,
Url => NameA => Paul
AmountA => 5
NameB => mark
AmountB => 8
NameB => Luke
AmountB =>2
)

what you want is an array of hashes or a hash of hashes could work too. Besides that I'm not real sure on what you are trying to do.
 
Hiya Kevin,

Yes it is real code obviously minusing the real url and names etc and each of the lines obviously ends with a comma.

 
if the order is important you need to use an array of hashes (I think). What you have posted above is really a hash but you are trying to push it into an array, so you are really just creating an array as far as I can tell. You need something like:

Code:
push(@myArray,
   {Url => '[URL unfurl="true"]www.nameofDomain1com',[/URL]
   NameA => 'Paul',
   AmountA => 5,
   NameB => 'mark',
   AmountB => 8,
   NameC => 'Luke',
   AmountC =>2,}
);

the curly brackets tell perl to make an anonymous hash and push it into the array. You can repeat the push process for as many anonymous arrays as you need to create within the array.
 
this:

You can repeat the push process for as many anonymous arrays as you need to create within the array.

should be:

You can repeat the push process for as many anonymous hashes as you need to create within the array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top