need help with list of lists ( multi dimensional array)

Discussion in 'Perl Misc' started by news-acct, Aug 25, 2005.

  1. news-acct

    news-acct Guest

    Hi - looking for pointers or links to resources ...

    I am reading in a text file - lines are
    prod_id cost sn
    Prod1 1000 123
    Product1 1000 124
    Product1 2000 456
    Prod1 2000 457
    Product2 1000 323
    Prod2 1000 324
    Product2 2000 556
    Prod2 2000 557
    Product3 1000 223
    Prod3 1000 224
    Product3 2000 756

    I need to count how many of each product, and how many of each product AND
    cost.
    Count of Product1 - 1000: 2
    Count of Product1 - 2000: 2
    Total count of Product1: 4

    Count of Product2 - 1000: 2
    Count of Product2 - 2000: 2
    Total count of Product2: 4

    Count of Produc3 - 1000: 2
    Count of Product3 - 2000: 1
    Total count of Product3: 3

    Total count of all products: 11

    It seems to be that an array would work great here, but I am not successful
    at doing it.
    would like to have an array -
    array_cnt[Product_id][cost_value][count]
    where I could just increment the [count] value.
    Thanks.
    news-acct, Aug 25, 2005
    #1
    1. Advertising

  2. news-acct

    Paul Lalli Guest

    news-acct wrote:
    > Hi - looking for pointers or links to resources ...


    perldoc perllol - "Lists of Lists"
    perldoc perldsc - "Data Structures Cookbook"

    > I am reading in a text file - lines are
    > prod_id cost sn
    > Prod1 1000 123
    > Product1 1000 124
    > Product1 2000 456
    > Prod1 2000 457
    > Product2 1000 323
    > Prod2 1000 324
    > Product2 2000 556
    > Prod2 2000 557
    > Product3 1000 223
    > Prod3 1000 224
    > Product3 2000 756
    >
    > I need to count how many of each product, and how many of each product AND
    > cost.
    > Count of Product1 - 1000: 2
    > Count of Product1 - 2000: 2
    > Total count of Product1: 4


    I don't understand your data. Is "Prod1" equivalent to "Product1"?

    > Count of Product2 - 1000: 2
    > Count of Product2 - 2000: 2
    > Total count of Product2: 4
    >
    > Count of Produc3 - 1000: 2
    > Count of Product3 - 2000: 1
    > Total count of Product3: 3
    >
    > Total count of all products: 11
    >
    > It seems to be that an array would work great here, but I am not successful
    > at doing it.
    > would like to have an array -
    > array_cnt[Product_id][cost_value][count]
    > where I could just increment the [count] value.


    Why would you want to keep a separate count value? A Perl array in
    scalar context gives its size...

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dumper;

    my %products;
    while (<DATA>){
    my ($prod, $cost, $sn) = split ' ';
    $prod =~ s/uct//;
    push @{$products{$prod}{$cost}}, $sn;
    }

    for my $prod (sort keys %products){
    my $total = 0;
    for my $cost (sort {$a <=> $b} keys %{$products{$prod}}){
    my $count = @{$products{$prod}{$cost}};
    print "Count of $prod - $cost: $count\n";
    $total += $count;
    }
    print "Total count of $prod: $total\n";
    }


    __DATA__
    Prod1 1000 123
    Product1 1000 124
    Product1 2000 456
    Prod1 2000 457
    Product2 1000 323
    Prod2 1000 324
    Product2 2000 556
    Prod2 2000 557
    Product3 1000 223
    Prod3 1000 224
    Product3 2000 756


    Output:
    Count of Prod1 - 1000: 2
    Count of Prod1 - 2000: 2
    Total count of Prod1: 4
    Count of Prod2 - 1000: 2
    Count of Prod2 - 2000: 2
    Total count of Prod2: 4
    Count of Prod3 - 1000: 2
    Count of Prod3 - 2000: 1
    Total count of Prod3: 3

    Paul Lalli
    Paul Lalli, Aug 25, 2005
    #2
    1. Advertising

  3. news-acct

    news-acct Guest

    "Paul Lalli" <> wrote in message
    news:...
    > news-acct wrote:
    >> Hi - looking for pointers or links to resources ...

    >
    > perldoc perllol - "Lists of Lists"
    > perldoc perldsc - "Data Structures Cookbook"
    >

    [ .. lines snipped ..]
    > Paul Lalli
    >


    Thanks..
    news-acct, Aug 27, 2005
    #3
  4. news-acct

    news-acct Guest

    trying to understand this better...
    what if had 4 columns ( prod, cost, sn, loc_#)
    ___DATA___
    prod1 10 123 B45
    prod1 10 234 B45
    prod1 20 456 C54
    prod2 10 852 D55
    prod2 10 853 D55
    prod2 20 985 E54

    Want total count of all prod's, and count of prod# and amt
    prod1 10 count=2
    prod1 20 count=1
    prod2 10 count=2
    prod2 20 count =1
    Total count = 6

    Are you using an array of hashes or hashes of arrays? Thanks
    news-acct, Aug 28, 2005
    #4
  5. news-acct wrote:
    > trying to understand this better...
    > what if had 4 columns ( prod, cost, sn, loc_#)
    > ___DATA___
    > prod1 10 123 B45
    > prod1 10 234 B45
    > prod1 20 456 C54
    > prod2 10 852 D55
    > prod2 10 853 D55
    > prod2 20 985 E54
    >
    > Want total count of all prod's, and count of prod# and amt
    > prod1 10 count=2
    > prod1 20 count=1
    > prod2 10 count=2
    > prod2 20 count =1
    > Total count = 6
    >
    > Are you using an array of hashes or hashes of arrays? Thanks


    Maybe this version in Ruby will be easier to understand.
    It puts the information into a hash of hashes. After
    reading the data, prodprice looks like:

    {"prod1"=>{"20"=>1, "10"=>2}, "prod2"=>{"20"=>1, "10"=>2}}

    -------------------------------------------------------------

    prodprice = Hash.new { |hash, key| hash[key] = Hash.new(0) }

    DATA.each { |line|
    prod, price = line.split
    prodprice[prod][price] += 1
    }
    sum = 0
    prodprice.sort.each { |prod,hash|
    hash.sort.each { |price,count|
    puts "#{prod} #{price} #{count}"
    sum += count
    }
    }
    puts sum

    __END__
    prod1 10 123 B45
    prod1 10 234 B45
    prod1 20 456 C54
    prod2 10 852 D55
    prod2 10 853 D55
    prod2 20 985 E54
    William James, Aug 28, 2005
    #5
  6. news-acct

    Matt Garrish Guest

    "William James" <> wrote in message
    news:...
    >
    > news-acct wrote:
    >> trying to understand this better...
    >> what if had 4 columns ( prod, cost, sn, loc_#)
    >> ___DATA___
    >> prod1 10 123 B45
    >> prod1 10 234 B45
    >> prod1 20 456 C54
    >> prod2 10 852 D55
    >> prod2 10 853 D55
    >> prod2 20 985 E54
    >>
    >> Want total count of all prod's, and count of prod# and amt
    >> prod1 10 count=2
    >> prod1 20 count=1
    >> prod2 10 count=2
    >> prod2 20 count =1
    >> Total count = 6
    >>
    >> Are you using an array of hashes or hashes of arrays? Thanks

    >
    > Maybe this version in Ruby will be easier to understand.
    >


    The Ruby group is down the hall. Or are so few people using the language you
    have to post here?

    I have an urge now to go find a C++ group and start posting C# answers to
    everything. That'll learn them good!

    Matt
    Matt Garrish, Aug 28, 2005
    #6
  7. "Matt Garrish" <> wrote in
    news:m6iQe.56$:

    >
    > "William James" <> wrote in message
    > news:...

    ....
    >> Maybe this version in Ruby will be easier to understand.
    >>

    >
    > The Ruby group is down the hall. Or are so few people using the
    > language you have to post here?


    Nah, he's Xah Lee's long lost brother.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Aug 28, 2005
    #7
    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. Venkat
    Replies:
    4
    Views:
    952
    Venkat
    Dec 5, 2003
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    378
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. agent-s
    Replies:
    16
    Views:
    436
    Gerard Flanagan
    Feb 14, 2007
  4. Simon Brunning
    Replies:
    5
    Views:
    488
    Peter Otten
    Jun 9, 2007
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    193
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page