I need to develop a peice of code, a function, or object that builds a hash of arrays from a subset of arrays comming from a larger hash of arrays based on a list. My current application reads in a file and builds a few arrays from the elements of the file. For example, each line of the file has a country listed, a client, and a identifier. As I read in the file I populate arrays for each of these catagories with unique values. For the country catagory I need to know all of the countries that events happened in. here is the sub:
# ----Build_Country_List ----------------- sub Build_Country_List ($$) { my $j = shift; my $country = shift; print "build country list\n"; $count = 0; if ($j == 0) { print "initial loop\n"; $CountryList[$j] = $country; foreach $tradingcountry (@CountryList) { print "array $j $tradingcountry\n"; } $LengthofCountryList = 1; } else { foreach $tradingcountry (@CountryList) { if ($tradingcountry eq $country) $condition = "FALSE"; $count++; } else { $condition = "TRUE"; } } } if (($condition eq "TRUE") && ($count == 0)) { $LengthofCountryList = scalar(@CountryList); $CountryList[$LengthofCountryList] = $country; } foreach $tradingcountry (@CountryList) { print "country elements $tradingcountry\n"; } $LengthofCountryList = scalar(@CountryList); } # end sub Build_Country_List
I do the same thing for client, and identifier. What I would like to do is build a function which takes as imput a hash of arrays and uses this country array to extract array elements out of the larger array based on the elements of the country array. basically I want to use an array as a filter to build a reduced hash of arrays from a bigger one..... Another bit of code builds an hash of arrays (my first!) and I would like to use the structure produced to filter a larger array witrh the same idea as above. Any suggestions, or condensations, would be appreciated.
sub Build_totals_shares_for_sedol_Sell_side($$$) { #$j = shift; my $sedol = shift; my $shares = shift; $condition = ""; my $mySellfirstflag = shift; if ($mySellfirstflag eq "YES") { my $j = 0; } else { my $j = 1;} print "$sedol $shares\n"; $count = 0; if ($j == 0) { %Sellhash = ($sedol => $shares); #print "$Sellhash{key} \n"; foreach $sedolelement (keys %Sellhash) { print "Sell: The $sedolelement has traded\n"; foreach (@{$Sellhash{$sedolelement}}) { print "$_\n"; } } #$NumTradingSedols = 1; } else { #print "this is j $j\n"; foreach $sedolelement (keys %Sellhash) { #print "the key is: $sedolelement te sedol is $sedol\n"; if ($sedolelement eq $sedol) { $condition = "TRUE"; print "the sedol is in the list so add the shares.\n"; $usekey = $sedolelement; $oldshares = $Sellhash{$sedolelement}[1]; } else { $condition = "FALSE"; $count++; } } if (($condition eq "FLASE") && ($count != 0)) { $Sellhash{"$sedol"} .= $shares; print "the new entry is $sedol $shares\n"; } else { $Sellhash{"$sedol"} = $Sellhash{"$sedol"} + $shares; $newtotal = $Sellhash{"$sedol"}; print "the new total shares for sedol: $sedol is $newtotal +\n"; } foreach $sedolelement (keys %Sellhash) { $total = $Sellhash{"$sedolelement"}; } } }
Has anyone done this..... where should I start .... I appologize for my novice in advance! Evan

In reply to Building a Hash of arrays from a hash of arrays based on a list? by evangraj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.