On Tue, 12 Jun 2001, F.H wrote:
>
> Hi All,
> I have a text file like this:
>
> Name ,123-43-4352, JX, 1234
> Sports,123-43-4352, SKI, BaseBall, swimming
> Hobbies, 123-43-4352, H1,H2, H3
> Hobbies, 123-43-4352, HH, HHH, HHHH2
> Hobbies,123-43-4352, H1,H43
>
> Name ,223-63-9352, JX, 1234
> Sports,223-63-9352, SKI, BaseBall, swimming
> Hobbies, 223-63-9352, H1,H2, H3
> Hobbies, 223-63-9352, HH, HHH, HHHH2
> Here is my issue, I am trying to access each record in this file by SSN
> ie: (223-63-9352)
> I want to be able to say:
> for SSN = 223-63-9352, for Hobbies4 4th entry is HGFR.
You will want to learn how to use hashes instead of just using
arrays. Give the following (untested code) a try:
# Gather the data
my $data = {};
while ($line = <INPUT>) {
chomp $line;
my ($key, $ssn, @values) = split /\s*,\s*/, $line;
print STDERR "Unparsable record: $line\n"
unless $key =~ /^(name|sports|hobbies)$/i;
$data->{$ssn}->{lc $key} = @values;
}
# print the data
foreach my $ssn (keys %$data) {
print "SSN: $ssn\n";
print "Name: ", join ', ', @{$data->{ssn}->{name}}, "\n";
print "Hobbies: ", join ', ', @{$data->{ssn}->{hobbies}}, "\n";
print "Sports: ", join ', ', @{$data->{ssn}->{sports}}, "\n";
print "\n";
}
Like I said, this is untested code , but it should give you an idea of
what you can do.
Cees
>
> while ($line = <INPUT>){
>
>
> @line = &parse_line(',',0,$line);
> if ($line[0] =~ /^NAME/gi){
> $ssn = $line[1] ;
> push(@ssns, $ssn);
>
> }
>
> if ($line[0] =~ /^Sports/gi ){
>
> push(@sports, $line)
>
> }
>
> if ($line[0] =~ /^Hobbies/gi){
>
> push(@hobbies, $line)
>
> }
>
> for ($i = 0; $i<= $#ssns; $i ++)
> {
> print "For $ssns[$i]\n";
>
> for ($i = 0; $i <= $#Hobbies ; $i++){
>
> if ($Hobbies[$i] =~ $ssns[$i])
> { print "Hobbies are: $Hobbies[$i]\n";}
> }
>
>
> I wanted to print out for each SSN corresponding SSN numbers but it's just
> not working!!! If this is feasible in Perl I'd appreciate if someone could help.
>
>
> Thanks
> I.S
>
> __________________________________________________________________
> Get your own FREE, personal Netscape Webmail account today at
>http://webmail.netscape.com/
>
--
Cees Hek
SiteSuite Corporation
[EMAIL PROTECTED]