Hi All,

Here is my problem with the script below:
 I can access each item from each line. But I got stuck trying to figure
 out how to access each hobbies/sports lines!!
 I need to be able to get data from for instance:

 $ssns{$ssn}{hobbies}[0][2] which should yield H3
 $ssns{$ssn}{hobbies}[3][0]   to get HG.

(data file is at bottom of message)

In a loop:                   
for ssn = xyz:

 Hobbies are:
 line 0, row 0 : =
 line 0, row 1 : =
 line 1, row 0: =
 line 1, row 1: =
 and so on.



 #### script is below
use strict;
use Data::Dumper; $Data::Dumper::Indent=1; # print &Data::Dumper::Dumper($var);

my %ssns;

while (<DATA>) {

#   chomp;          # remove \n
    next if /^\s*$/;    # skip blanke lines

    my @line = split /\s*,\s*/, $_;
    my $key = $line[0];
    my $ssn = $line[1];

    # Name, SSN, <??>, <??>

    if ($key =~ /^Name/i) {
        $ssns{$ssn}{name} = $line[2];
        $ssns{$ssn}{number} = $line[3];
    }

    # Sports, SSN, <sport>, ...

    if ($key =~ /^Sports/i) {
        push @{$ssns{$ssn}{sports}}, @line[2 .. $#line];
    }

    # Hobbies, SSN, <hobby>, ...

    if ($key =~ /^Hobbies/i) {
        push @{$ssns{$ssn}{hobbies}}, @line[2 .. $#line];
    }
}
#print Data::Dumper->Dump([\%ssns], [qw(ssns)]);    # dump hash

# print results

foreach (sort keys %ssns) {

    print "For ssn $_:\n";

    print "  Sports are:\n    ";
    foreach (sort @{$ssns{$_}{sports}}) {
        print "$_ ";
    }
    print "\n";

    print "  Hobbies are:\n    ";
    foreach (sort @{$ssns{$_}{hobbies}}) {
        print "$_ ";
    }
    print "\n";
    print "\n";
}

exit 0;

__DATA__
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
Hobbies,223-63-9352, H1,H43
Hobbies,223-63-9352, HG, HG, HGFR

__END__
I.S
__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at 
http://webmail.netscape.com/

Reply via email to