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

@Rob: The structure of my data is like

category_a->index_a->value_a
category_a->index_b->value_b
category_a->category_b->index_a->value_a

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:

[code]
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;
}
[/code]


Greetings
Dennis
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:
<value1>:<value2>:<value3>...

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:
$hash->{value1}->{value2}...

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 :(
}
}

Greetings
Dennis

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.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?
Regards,
Rob

Search Discussions

Discussion Posts

Previous

Related Discussions

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

People

Translate

site design / logo © 2022 Grokbase