I
Ian Petts
I have a series of Squid logs I need extract last-used dates from for
a set of users.
I want to read in a list of usercodes and user names from a file, then
go through the squid logs and for each user found, add a 'last used'
date to the user list.
I am struggling to get my head around Perl's data structures. I am not
sure whether I need a hash of arrays, array of hashes or a hash of
hashes.
I have been fumbling around trying to work out how to [best] create
and maintain the proper data structure for this situation, but my head
is spinning. The following seems to work to a point, but I can't work
out how to change the date value after it is initially created. Here
is what I've come up so far:
#!/usr/bin/perl -w
# Temporarily disabled 'strict' as it increased my confusion while
# coming to terms with this.
#use strict;
use diagnostics;
# Create some base data. (Not required, but all part of the
# learning process).
@userdata = (
{
user => "jsmith",
desc => "John Smith",
date => 0,
},
{
user => "gjones",
desc => "Greg Jones",
date => 0,
},
);
# I can add users to the list, like this:
push @userdata, { user => "suser", desc => "Steve User", date => 0 };
push @userdata, { user => "ipetts", desc => "Ian Petts", date => 0 };
push @userdata, { user => "pcitizen", desc => "Pete Citizen", date =>
0 };
# Let's have a look and see if I have what I think I do.
# This should print the lot:
for $href (@userdata) {
print "{ ";
for $thing ( keys %$href ) {
print "$thing=$href->{$thing} ";
}
print "}\n";
}
print "\n\n";
# And this should print out whatever field I want.
# But how do I get a specific user?
for $href (@userdata) {
print $href->{"user"} . " ";
print $href->{"desc"} . " ";
print $href->{"date"} . "\n";
}
print "\n\n";
# Now let's assume I've found this user in the log and want to update
# their last-used date in the list.
# This is wrong :-(
$userdata->{"user"}->{"ipetts"}->{"date"} = 12345 ;
# And print it out for a look.
for $href (@userdata) {
print "{ ";
for $thing ( keys %$href ) {
print "$thing=$href->{$thing} ";
}
print "}\n";
}
Could someone please point me in the right direction?
Thanks,
Ian.
a set of users.
I want to read in a list of usercodes and user names from a file, then
go through the squid logs and for each user found, add a 'last used'
date to the user list.
I am struggling to get my head around Perl's data structures. I am not
sure whether I need a hash of arrays, array of hashes or a hash of
hashes.
I have been fumbling around trying to work out how to [best] create
and maintain the proper data structure for this situation, but my head
is spinning. The following seems to work to a point, but I can't work
out how to change the date value after it is initially created. Here
is what I've come up so far:
#!/usr/bin/perl -w
# Temporarily disabled 'strict' as it increased my confusion while
# coming to terms with this.
#use strict;
use diagnostics;
# Create some base data. (Not required, but all part of the
# learning process).
@userdata = (
{
user => "jsmith",
desc => "John Smith",
date => 0,
},
{
user => "gjones",
desc => "Greg Jones",
date => 0,
},
);
# I can add users to the list, like this:
push @userdata, { user => "suser", desc => "Steve User", date => 0 };
push @userdata, { user => "ipetts", desc => "Ian Petts", date => 0 };
push @userdata, { user => "pcitizen", desc => "Pete Citizen", date =>
0 };
# Let's have a look and see if I have what I think I do.
# This should print the lot:
for $href (@userdata) {
print "{ ";
for $thing ( keys %$href ) {
print "$thing=$href->{$thing} ";
}
print "}\n";
}
print "\n\n";
# And this should print out whatever field I want.
# But how do I get a specific user?
for $href (@userdata) {
print $href->{"user"} . " ";
print $href->{"desc"} . " ";
print $href->{"date"} . "\n";
}
print "\n\n";
# Now let's assume I've found this user in the log and want to update
# their last-used date in the list.
# This is wrong :-(
$userdata->{"user"}->{"ipetts"}->{"date"} = 12345 ;
# And print it out for a look.
for $href (@userdata) {
print "{ ";
for $thing ( keys %$href ) {
print "$thing=$href->{$thing} ";
}
print "}\n";
}
Could someone please point me in the right direction?
Thanks,
Ian.