I need to join records
am 02.08.2005 23:17:37 von tech1
I have data that is has different information in the same data field. When
I export the data from my database it creates a duplicate record with
different information in the indicated field. I need to "consolidate all
those records into one record per student.
Thanks for any help, hints or other.
Alf
Re: I need to join records
am 03.08.2005 14:38:46 von Paul Lalli
tech1 wrote:
> I have data that is has different information in the same data field. When
> I export the data from my database it creates a duplicate record with
> different information in the indicated field. I need to "consolidate all
> those records into one record per student.
>
> Thanks for any help, hints or other.
This is not a particularly well-defined problem description. I'm
making a few assumptions about what you want to do. They are:
You receive several records of input.
Fields in each record are |-delimited.
Each record has one key field, an identifier of "students"
The N'th field of each record contains a piece of data you want
associated with the student.
You do not know how many records you have.
You do not know how many records per student you have.
You do not have a way of "labeling" each piece of data in the Nth field
You do not care which order your students are saved in.
Using all those assumptions, here's my suggestion:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %students;
while (){
my ($student, $info) = (split /\|/)[0,1];
push @{$students{$student}}, $info;
}
print Dumper(\%students);
__DATA__
Paul|123 Main St|xxx|yyy
Paul|84.7%|xxx|yyy
John|456 East Ave|xxx|yyy
John|98%|xxx|yyy
Paul|freshman|xxx|yyy
John|senior|xxx|yyy
Output:
$VAR1 = {
'Paul' => [
'123 Main St',
'84.7%',
'freshman'
],
'John' => [
'456 East Ave',
'98%',
'senior'
]
};
Hope this helps get you started.
Paul Lalli