Thanks for the codingstyle tips, Shlomi. I instantly changed that =)

@Rob: The structure of my data is like


So sometimes I have a category in a category, sometimes it is just a
value in a category. I think in my case it is better to have a
hash-monster like that. I feel more comfortable with this style of
datastructure. But if anyone has a better idea to handle this, I'd be
glad to hear =)

Now to the code itself: I forgot to mention that its not actually a
hash, its an object. Sorry for that mistake. That might be the reason
why the code Shlomi gave to me doesn't work as intented. The $hash_ref
has some values in it, but not in the correct structure and not every
line from the file read finds its way to the hash_ref. Now thats what
I got so far:

sub read_log {
my $self = shift;
my $filename = get_filename();
print "\n$filename\n";

open my $log, "<", $filename or die "Error opening file: $!";

while (my $line = <$log>) {
chomp $line;
my @values = split /:/, $line;
my $hash_ref = $self;
foreach my $val (@values[0 .. $#values-2])
$hash_ref = ($self->{$val} ||= {});

$hash_ref->{$values[-2]} = $values[-1];

print Dumper $self;

2010/12/1 Rob Coops <rcoops@gmail.com>:
On Wed, Dec 1, 2010 at 10:24 AM, Dennis Jakobi wrote:

Hi there,

I have the following problem:

I want to read a logfile in which every line follows this rule:

But the number of values differs. Sometimes a line has 2 values (the
minimum) and sometimes 3 or more values. Now I want to push these
values into a hash that follows that form:

This is what I have so far:

sub readLog {
my $self = shift;
my $logfile = getFilename();

open LOG, "<$logfile";

foreach my $line (<LOG>) {
my @values = split /\:/, $line;
... # at this point I don't know what to do :(


To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
Hi Dennis
I know a weird question that you likely already thought about but still why
are you not using a combination of array and hash?
$hash->{value1} = [ value2, value3, value4, ... ]
It would save you a lot of headaches in looping over all values that are on
that one line because how are you going to easily handle a situation where
you have a hash of hashes with an undefined depth without having to jump to
a lot of difficult hoops to make it work properly?

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 4 | next ›
Discussion Overview
groupbeginners @
postedDec 1, '10 at 9:24a
activeDec 1, '10 at 10:56a



site design / logo © 2022 Grokbase