Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to PERL (i started reading my first tutorial yesterday afternoon :)), and this is what i'd like to do (i've been looking everywhere for similar examples but cant quite find it)

I'd like to take a comma de-limited text file and make each line in the text file it's own array with the first item in each array the name of the array (at least thats how I think it should be done). However the first item in the line isn't always unique and the same number of items aren't in every line (some line may have 3, others 6). The good thing is that there are only 6 possible things the first item in each line could be.
ex. Andrea, 6, good, nice, 3 Bill, 5, 4, better, 5, 7, 4 Clark, 4, 5 Dave, best, better, best Joe, 5, 0 Joe, 4, 6 William, 6, 7, 8, 3 , 1 ,5 Andrea, 45, bettr, best, 6 where the result I would like is a matrice ex. Andrea(1, 2)=good Dave(1,2) = better Joe(1,2) = 0
where the first subscript is the nth number of the same thing and the second is the item number in the array. Can you help me in figuring out how to do this/give me suggestions. THanks!!!

BTW, I hope i'm not asking for too much :)

Replies are listed 'Best First'.
Re: Making an array out of each line of a text file
by merlyn (Sage) on Aug 25, 2000 at 20:49 UTC
    Well, here's one way to do it.
    while (<DATA>) { chomp; my @line = split /,\s*/; my $key = shift @line; push @{$data{$key}}, \@line; } print $data{"Andrea"}[0][1], " should be good\n"; # offsets off by one print $data{"Dave"}[0][1], " should be better\n"; print $data{"Joe"}[0][1], " should be 0\n"; __END__ Andrea, 6, good, nice, 3 Bill, 5, 4, better, 5, 7, 4 Clark, 4, 5 Dave, best, better, best Joe, 5, 0 Joe, 4, 6 William, 6, 7, 8, 3 , 1 ,5 Andrea, 45, bettr, best, 6

    -- Randal L. Schwartz, Perl hacker

      This is what I came up with (this is the whole script) and i really dont know what to do to make it to work. I've been trying to mess around with stuff but it's not working.. :(

      I'm looking at other wayts to write it but none of them seem as effecient (with as less lines of code) as yours. Could you quickly look through it and see if any errors/screw ups pop out at you? Your help is greatly appreciated (..and I thought Perl was going be easy as cake :)

      #!/usr/local/bin/perl $DATA='data.txt'; open (DATA,"<$DATA") || die "Can't open $DATA $!"; @line = <DATA>; foreach (@line) { chop; ($key, $one, $two, $three, $four, $five, $six) = (split(/,/)); $key = "" if !defined($key); #this should make it null if nothing +'s there $one = "" if !defined($one); $two = "" if !defined($two); $three = "" if !defined($three); $four = "" if !defined($four); $five = "" if !defined($five); $six = "" if !defined($six); } while (<DATA>) { chomp; @line = (split(/,/)); # was getting errors on split /,\s*/; $key = shift @line; push @{$data{$key}}, \@line; } print $data{"Andrea"}[0][1], " should be good\n"; # offsets off by one + + print $data{"Dave"}[0][1], " should be better\n"; print $data{"Joe"}[0][1], " should be 0\n"; close(DATA);
Re: Making an array out of each line of a text file
by xdb19 (Sexton) on Aug 25, 2000 at 22:08 UTC
    The correct code is Professor Schwartz's code, evaluate that. I can see you tried to copy it, but there are a few mistakes. As a newbie to Perl, it is important to know what you did incorrectly. I hope this helps you with future Perl. As that is the most important thing, that you get a good understanding of perl.
    #!/usr/local/bin/perl $DATA='data.txt'; open (DATA,"<$DATA") || die "Can't open $DATA $!"; # # Why do this??? # # @line = <DATA>; #foreach (@line) { # # for future reference, chop is unsafe ( it cuts off the last # character, no matter what it is ) use chomp instead, it # takes off all of the trailing whitespace. # # chop; # #the brackets around split are unnecessary # # ($key, $one, $two, $three, $four, $five, $six) = (split(/,/)); # # This is completely unnecessary ( since they are already null if they + do not exist ) # the only things you might get are warnings of uninitialized variable +s. # # $key = "" if !defined($key); #this should make it n +ull if nothing's there # $one = "" if !defined($one); # $two = "" if !defined($two); # $three = "" if !defined($three); # $four = "" if !defined($four); # $five = "" if !defined($five); # $six = "" if !defined($six); #} # # Notice Code, ALA Schwartz # #while (<DATA>) { # chomp; # my @line = split /,\s*/; # my $key = shift @line; # push @{$data{$key}}, \@line; #} # while (<DATA>) { chomp; # # Once again brackets around split are totally unnecessary # # # if you do not put my in front of @line, when you # push the reference (\@line), onto the array in the hash, # you will get a reference to the same array over and over # # also would be good to put my in front of $key, although # not necessary # @line = split(/,\s*/); # was getting errors on split / +,\s*/; $key = shift @line; push @{$data{$key}}, \@line; } print $data{"Andrea"}[0][1], " should be good\n"; # offset +s off by one print $data{"Dave"}[0][1], " should be better\n"; print $data{"Joe"}[0][1], " should be 0\n"; # # Good programming Style # close(DATA);

    I hope this helps
    - Have Fun, XDB19
Re: Making an array out of each line of a text file
by Cirollo (Friar) on Aug 25, 2000 at 20:49 UTC