Hello Monks,
I'm hoping you can yet again help me..wait I know you "can" help me let me rephrase that. I hope that you will seek to enlighten me with your infinte wisdom.

I have a hash that is made of up the following elements:

1958796238 => ch02.03.MultipleChoice 029
1631977192 => ch02.03.MultipleChoice 012
649020496 => ch01.03.MultipleChoice 061
1431783758 => ch02.03.MultipleChoice 082
1515026861 => ch01.03.MultipleChoice 071
1767673050 => ch01.03.MultipleChoice 049
1988528379 => ch01.03.MultipleChoice 091
220401953 => ch01.03.MultipleChoice 071


This list is not complete as it is a very large pull form a db. This is just a sample. The Key value is unique meaing that only one Key value can be associated with one Hash value (But not vice versa). I need to create an array that will have the key value for each unique key. That is the array will need each key that has ch01.03.MultipleCHoice 071 as it's value. Then I will need to repeat this process (with some stuff done in between) for each unique key value (and it's associated keys). Below is the entire script.
#!/usr/bin/perl use strict; use warnings; use DBD::mysql; ###################### #### CONFIG VARS ##### ###################### #Specify start and end dates to filter data my $start_date = '2008-12-25'; my $end_date = '2009-04-01'; my $output_file = 'Hordis_Item_Overview.txt'; ###################### #### PRIVATE VARS #### ###################### my $db_user = 'xxxxx'; my $db_pass = 'xxxxx'; ###################### #### MAIN BODY ####### ###################### open OUTPUT, ">$output_file"; my $dbh = DBI->connect("DBI:mysql:database=xxxx:host=xxxxx",$db_user,$ +db_pass,{RaiseError=>1})|| die "$DBI::errstr\n"; #Hash to hold ItemUID and Item Name my %ItemUID= (); #Get ItemUID and Item Name (ItemUID is unique to each Test.IData) { my $sql = "SELECT t.IData FROM Tests AS t JOIN Assignments AS a ON a.A +ssignmentTestID=t.ID JOIN Users AS u ON u.ID=t.UserID WHERE u.Institu +tionID='004452' AND t.IData REGEXP 'ch01.03.MultipleChoice 049';"; my $sth = $dbh->prepare ( $sql ); $sth->execute(); while (my @row = $sth->fetchrow_array()) { my $idata = $row[0]; while ($idata =~ /<item([^>]+)/gi) { my $elements = $1; my $src; my $uid; if ($elements =~ /src=\"\w+\/([^"]+)/i) { $src = $1 } if ($elements =~ /uid=\"([^"]+)/i) { $uid = $1 } if (not exists $ItemUID{$uid}) { $ItemUID{$uid} = $src } } } $sth->finish(); } #Hold Unique Item Names to use against ItemUID hash @hvalues=(); while (my ($key, $value) = each $ItemUID) { push #Get Item Specific Info for Each Student/Assignment { while (my ($key, $value) = each %ItemUID) { print "Processing $key/$value\n"; my $sql = "SELECT s.Name, CONCAT(u.LastName,', ',u.FirstName), a.N +ame, TRUNCATE((MAX(air.TimeSpent)/60),0), TRUNCATE((MIN(air.TimeSpent +)/60),0), SUM(air.TimesTaken), AVG(air.Score), air.FeedbackAttempts, +air.HintsGiven FROM Sections AS s JOIN Assignments AS a ON s.ID=a.SectionID JO +IN Users AS u ON u.ID=s.OwnerID JOIN AssignmentResults AS ar ON a.ID= +ar.AssignmentID JOIN AssignmentItemResults AS air ON ar.ID=air.Assign +mentResultsID WHERE air.ItemUID IN '$key' GROUP BY s.Name ORDER BY s.Name, a. +Name, u.LastName"; my $sth = $dbh->prepare ( $sql ); $sth->execute(); while (my @row = $sth->fetchrow_array()) { print OUTPUT "$row[0]\t$row[1]\t$row[2]\t$row[3]\t$row[4]\t$ro +w[5]\t$row[6]\t$row[7]\n"; } $sth->finish(); } } $dbh->disconnect(); close OUTPUT; ###################### #### PRIVATE SUBS #### ######################

In reply to Creating Array from Hash that pulls only Unique values while iterating over each Unique Value by kdmurphy001

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.