in reply to oh boy!
Allright, let's put it straight, I don't like to write other's code as I don't like others to write the code I'm in charge with. But, having said that, and having talk with qball in the CB, his problem was more interresting than it first sounded as he couldn't use a proper database to do the job (which obviously, would have been the best solution)
So, after having toyed with the idea of using DBD::RAM, it seemed that this very cool module wasn't able to fill the task (which I doubt, but I haven't tried to do it with it...)
So, here's my code:
#!/usr/bin/perl -w use strict; my $datas = {}; my %types; ##open needed files to use in program open(NAMES, "file1.csv") or die "Can't open NAMES file: $!\n"; open(TYPES, "file2.csv") or die "Can't open TYPES file: $!\n"; open(INFO, "file3.csv") or die "Can't open INFO file: $!\n"; # Process INFO while (<INFO>){ chomp; my ($unused_col, $type_name, $name_col) = split(/,/, $_, 3); $types{$type_name} = undef; # We never knows from this table where + # these fields are linked from? } close INFO; # Process NAMES while (<NAMES>){ chomp; my ($name_id, $c_name) = split(/,/, $_, 2); $datas->{$name_id} = {'c_name' => $c_name } # provided the id i +s unique } close NAMES; # Process TYPES while (<TYPES>){ chomp; my ($type_id, $name_id, $type_name) = split(/,/, $_, 3); push @{$datas->{$name_id}->{'type_name'}}, $type_name; # Not sure if we have to test the existence of the type. # Can't hurt too much (and avoid undesired autovivification?) $types{$type_name}++ if exists $types{$type_name}; } close TYPES; # Not seen output print "Not seen:\n"; foreach (keys %types){ print "$_\n" unless $types{$_} } # Names count print "Names total count:\n"; foreach (keys %{$datas}){ print $datas->{$_}->{'c_name'} . ": " . scalar(@{$datas->{$_}->{'type_name'}}) . "\n"; }
I guess there's a cleaner implementation of this. Mmmh, well I'll get back to DBD::RAM, I'm pretty sure it could do the job just fine...
Cheers,
-- Briac
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Oh boy! A (mildly) complex data structure when there's no DB around
by qball (Beadle) on Mar 27, 2001 at 09:31 UTC |