Please help.....not understanding hases

H

HM

Here is my scenario, I have two variables, an account number and an
array of dollar prices. I am storing them in an array such as:

my @values = (1, 2, 3, 4, 5, 6);
my $accno = 55;
my $n = 6;

my @funds = (
{'acc'=>$accno, 'dol'=>[@values], ‘num’=>6},);

I will be adding more (unique) account numbers and their associated
dollar values to my hash in the program. My problem is I am having
trouble walking through @values (one by one) based on accno and
storing them in another array (@tmp). I appreciate any help with
this.
 
E

Ethan Brown

HM> Here is my scenario, I have two variables, an account number and an
HM> array of dollar prices. I am storing them in an array such as:

HM> my @values = (1, 2, 3, 4, 5, 6);
HM> my $accno = 55;
HM> my $n = 6;

HM> my @funds = (
HM> {'acc'=>$accno, 'dol'=>[@values], ‘num’=>6},);

HM> I will be adding more (unique) account numbers and their associated
HM> dollar values to my hash in the program. My problem is I am having
HM> trouble walking through @values (one by one) based on accno and
HM> storing them in another array (@tmp). I appreciate any help with
HM> this.

Hi HM--

The following works, but I also give an implementation using a hash instead of a
list for your storage structure, as it seems like it would be more appropriate.

Hope this helps,

--Ethan Brown
--Keyboards: "The Fabulous Pelicans" (www.pelicans.com)
--In a band? Use http://www.WheresTheGig.com for free.
===========================================================================

#!/usr/local/bin/perl -w
##
use strict;

my @values1 = (1, 2, 3, 4, 5, 6);
my @values2 = (2, 4, 6, 8, 10, 12);
my $accno1 = 55;
my $accno2 = 65;
my $n = 6;

my @funds = (
{'acc'=>$accno1, 'dol'=>[@values1], '‘num’'=>6},
{'acc'=>$accno2, 'dol'=>[@values2], '‘num’'=>6});
##----------------------------------------------^Had to quote this^

## Store values2 (from accno2) in array @tmp

my @tmp;
foreach (@funds)
{
if ($_->{acc} == $accno2)
{
@tmp = @{$_->{dol}};
last;
}
}

print "Array search: @tmp\n";

## There's probably a better way to do what you want. Why not a hash
## keyed by account numbers?

my %funds = ($accno1 => {'dol'=>[@values1], '‘num’'=>6},
$accno2 => {'dol'=>[@values2], '‘num’'=>6});

## Now we don't have to search an array:

@tmp = @{$funds{$accno2}->{dol}};

print "Tasty hash: @tmp\n";
 
G

Greg Bacon

: Here is my scenario, I have two variables, an account number and an
: array of dollar prices. I am storing them in an array such as:
:
: my @values = (1, 2, 3, 4, 5, 6);
: my $accno = 55;
: my $n = 6;
:
: my @funds = (
: {'acc'=>$accno, 'dol'=>[@values], ‘num’=>6},);

Consider the following example:

#! /usr/local/bin/perl

use strict;
use warnings;

my @funds = (
{ acc => 55, dol => [1 .. 6], num => 6 },
{ acc => 42, dol => [7 .. 9], num => 3 },
);

foreach my $f (@funds) {
my $acc = $f->{acc};
my $dol = $f->{dol};
my $num = $f->{num};

print "Account #$acc ($num):\n";
print " - $_\n" for @$dol;
}

You also could have accessed the array without the temporary using
a dereference such as the one below:

print " - $_\n" for @{ $f->{dol} };

See the perlref manpage for the fundamentals and the perllol and perldsc
manpages to see them in action.

: I will be adding more (unique) account numbers and their associated
: dollar values to my hash in the program. My problem is I am having
: trouble walking through @values (one by one) based on accno and
: storing them in another array (@tmp). I appreciate any help with
: this.

Maybe you should rethink your datastructure. In the context of Perl,
when you think of uniqueness, hashes should be the very next thought:

#! /usr/local/bin/perl

use strict;
use warnings;

my %fund = (
55 => { dol => [1 .. 6], num => 6 },
42 => { dol => [7 .. 9], num => 3 },
);

foreach my $acc (sort { $a <=> $b } keys %fund) {
my %f = %{ $fund{$acc} };

print "Account #$acc ($f{num}):\n";
print " - $_\n" for @{ $f{dol} };
}

Hope this helps,
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top