Please help.....not understanding hases

Discussion in 'Perl Misc' started by HM, Aug 28, 2003.

  1. HM

    HM Guest

    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.
    HM, Aug 28, 2003
    #1
    1. Advertising

  2. HM

    Ethan Brown Guest

    >>>>> "HM" == HM <> writes:

    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";
    Ethan Brown, Aug 28, 2003
    #2
    1. Advertising

  3. HM

    Greg Bacon Guest

    In article <>,
    HM <> wrote:

    : 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
    --
    How can an actor who attempts to convey every conceivable emotion by
    quickly blinking his eyes maintain a career for 25 years?
    -- Paul Tatara of Richard Gere
    Greg Bacon, Aug 28, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    1
    Views:
    360
    bruce barker
    Oct 4, 2004
  2. Jeff Wisnia
    Replies:
    6
    Views:
    393
    Jeff Wisnia
    Apr 6, 2005
  3. KK
    Replies:
    2
    Views:
    498
    Big Brian
    Oct 14, 2003
  4. Replies:
    9
    Views:
    426
    James Kanze
    Feb 12, 2008
  5. mdh

    Help with understanding this please

    mdh, Aug 30, 2008, in forum: C Programming
    Replies:
    14
    Views:
    512
    Keith Thompson
    Aug 31, 2008
Loading...

Share This Page