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

That is the array will need each key that has ch01.03...

If I understand correctly, you have pairs of UID and NAME. There will be several UIDs associated with each name. You want a list of all the unique names and for each name a list of all the UIDs associated with that name. The structure you need for this is a hash of arrays, and you can produce it at the same time you produce your ItemUID hash, as follows:

my %ItemUID = (); my %ItemName = (); #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; push(@{$ItemName{$src}}, $uid); } } } $sth->finish(); } # you can generate an array of the unique names if you like my @names = sort keys %ItemNames;

The hash $ItemName will have keys like 'ch02.03.MultipleChoice 029' and the values will be references to arrays of UIDs.

Hope this helps...

  • Comment on Re: Creating Array from Hash that pulls only Unique values while iterating over each Unique Value
  • Download Code