Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining Like Elements

Status
Not open for further replies.

Yrrk

IS-IT--Management
Aug 22, 2004
180
0
0
US
I'm running a query againts 3 seperate databases to extract counts from each and push them into an array. Most people therefore end up appearing 3 times in the array. Is there an easy way to combine like elements in this array such that i get only 1 total for each user or do i need to write my own subroutine to traverse the array and add like elements into a new array?

Note some of the below initials appear more then once. I'm trying to come up with a way that each person appears only once and the count is the total of however many times they appeared in the array.
Code:
for each DB we push onto the array:

while (($UserName, $Role, $RestoreCount) = $sth->fetchrow)
{
  push @data, { user => "$UserName" ,
              role => "$Role" ,
              started => "$RestoreCount" };
}

then we output the array later:

  VH        Operator  158
  AP      Supervisor  172
 BDM        Operator  233
 PMJ        Engineer  246
  JR        Operator  294
 BDM        Operator  341
  CC        Operator  355
  TH        Operator  369
  MS        SysAdmin  375
 FAP      Supervisor  410
  JM        Engineer  429
  MS        SysAdmin  497
 DAN        Operator  599
 FAP      Supervisor  608
  CS        Operator  614
  BF        Operator  650
  JJ        Operator  772
  JJ        Operator  925
  PG        Operator  1147
  RM        Operator  1152
  JM        Operator  1156
  CC        Operator  1208
  JR        Operator  1283
  TH        Operator  1370
  TR        Operator  1440
 FAP      Supervisor  1536
  EB        Operator  1668
  KC        Operator  1982
  DH        Operator  2013
  VH        Operator  4230
 
this kind of thing?

Code:
[b]#!/usr/bin/perl[/b]

chomp (@array = <DATA>);

foreach (@array) {
  m/^ *([A-Z]+) *([A-Za-z]+) *(\d+)$/;
  $remembered{"$1"}++;
}

while (($key, $value) = each %remembered) {
  print "$key => $value\n";
}

__DATA__
  VH        Operator  158
  AP      Supervisor  172
 BDM        Operator  233
 PMJ        Engineer  246
  JR        Operator  294
 BDM        Operator  341
  CC        Operator  355
  TH        Operator  369
  MS        SysAdmin  375
 FAP      Supervisor  410
  JM        Engineer  429
  MS        SysAdmin  497
 DAN        Operator  599
 FAP      Supervisor  608
  CS        Operator  614
  BF        Operator  650
  JJ        Operator  772
  JJ        Operator  925
  PG        Operator  1147
  RM        Operator  1152
  JM        Operator  1156
  CC        Operator  1208
  JR        Operator  1283
  TH        Operator  1370
  TR        Operator  1440
 FAP      Supervisor  1536
  EB        Operator  1668
  KC        Operator  1982
  DH        Operator  2013
  VH        Operator  4230


Kind Regards
Duncan
 
i.e. use a hash to remember the initials - as it is named it can not be duplicated - it will just be incremented:-

$remembered{"$1"}++;


Kind Regards
Duncan
 
not sure if this is what you want or what duncan posted:

Code:
my %users = ();
while (my ($UserName, $Role, $RestoreCount) = $sth->fetchrow )
{
$users{$UserName} = { role => $Role, started => $users{$UserName}{started}+=$RestoreCount };
}
foreach my $ID (sort keys %users) {
   print "$ID $users{$ID}{role} $users{$ID}{started}\n";
}

keeps a running totel of $RestoreCount per unique user and outputs something like this:

Code:
AP Supervisor 172
BDM Operator 574
BF Operator 650
CC Operator 1563
CS Operator 614
DAN Operator 599
DH Operator 2013
EB Operator 1668
FAP Supervisor 2554
JJ Operator 1697
JM Operator 1585
JR Operator 1577
KC Operator 1982
MS SysAdmin 872
PG Operator 1147
PMJ Engineer 246
RM Operator 1152
TH Operator 1739
TR Operator 1440
VH Operator 4388
 
Hi Kevin

I couldn't quite suss out what was needed either

... and the count is the total of however many times they appeared in the array.

... i'm not sure!?


Kind Regards
Duncan
 
me either and since you had one possibility covered, I thought I would cover what I saw as the other possibility.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top