[ale] Organizing data
Chris Fowler
cfowler at outpostsentinel.com
Wed Apr 28 08:19:36 EDT 2004
On Wed, 2004-04-28 at 08:07, Jason Etheridge wrote:
> Chris Fowler wrote:
> > The problem is that the first row has an identical row after it with the
> > exception of user id and email. That is where the extra stuff comes
> > from. The unique key that I will reference is the alarm_id and so there
> > are 5 rows of the same alarm_id. In the alarm table there is only one
> > alarm_id but when you start grabing data from all over the database that
> > has relationships then you end up with more than one row for a single
> > alarm in the example at http://www.linxdev.com/out.html I need to
> > condense the first 5 rows of alarm 4 into a single data structure. Then
> > The same for 5, 6, 7, etc...
>
> So your main concern is eliminating data redundancy? And you don't want
> to just mimic the tables with hashes and arrays? I'm not sure I'm
> following you right.
I do not mind mimicing the table with hashes and arrays as long as I do
not create. For example in that whole table there are only 2 unique
user ids. I do not want to create a user object for each row. That
would yield way too many user objects that have the same data.
Here was one way I solved this problem:#
# Build our notification groups
#
my $dbase = DBI->connect($db_name, $db_user, $db_pass) or die
$DBI::errstr ;
my $res = $dbase->prepare($NG_SQL) or die $dbase->errstr ;
$res->execute or die $res->errstr ;
$res->bind_columns(\$cols[0], \$cols[1], \$cols[2], \$cols[3],
\$cols[4]) or die $res->errstr;
while ($res->fetch) {
$NG{ $cols[0] }{ $cols[1] }{ 'id'} = $cols[1];
$NG{ $cols[0] }{ $cols[1] }{ 'timeout'} = $cols[2];
$NG{ $cols[0] }{ $cols[1] }{ 'users'}{$cols[3]} = $cols[4];
}
The reason why 'users' is a set of hashes is because there can be more
than one. This seems to work but walking it is kludgy.
Here is my walking.
#
# Function walks the list of NG's
# and displays them
#
sub print_ng {
my $a_ref = shift;
foreach my $ref (sort keys %{$a_ref}) {
print "NG: $ref\n";
print " Escalation Groups: \n";
foreach my $ref2 (sort keys %{$a_ref->{ $ref }}) {
foreach my $ref3 (sort keys %{$a_ref->{ $ref }->{ $ref2}}) {
if ( $ref3 =~ /users/) {
print " Users: ";
foreach my $ref4 (sort keys
%{$a_ref->{$ref}->{$ref2}->{'users'}}) {
print " $a_ref->{$ref}->{$ref2}->{'users'}->{$ref4} ";
}
print "\n";
} else {
print " $ref3 = $a_ref->{$ref}->{$ref2}->{$ref3}\n";
}
}
}
print "\n";
}
}
When I get into the hash of hashes I have to know if a key points to a
value or another hash.
So I want to mimic the data I just want to do it right.
More information about the Ale
mailing list