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

need to open a text file that has rows and colums (tab delimited), look for a name (in the first column), get the row that has the name so i can do stuff to it. i know it's simple, but i'm a beginner and not finding any examples of exaclty this. i don't want to search by row number, but by the name in the column.

Replies are listed 'Best First'.
(Ovid - homework?) Re: hash/array
by Ovid (Cardinal) on Jan 26, 2001 at 00:15 UTC
    malaga: may I ask what you plan to use this for? In reading through your posts, they seem so painfully vague that I can't help but suspect that you are asking us for help with homework. That's a bit of a touchy point here.

    From your past code, you appear to know how to open files. What you seem to be missing is that you should probably use split to get the data you want and then test the first element of the array that's created. I started writing a skeleton of the program, only to realize that I'm not sure that this is appropriate. If you do need assistance with homework, that's fine. Just let us know so that we can offer advice, rather than answers. Otherwise, you're cheating yourself in addition to your professor.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      no, not homework. work work. i would post the code i have, but i've changed it so many times i don't know where i'm at. i just need to get that one row from the text file, then turn it into html.
        malaga wrote:
        ...but i've changed it so many times...
        With all due respect, this implies that you probably started coding your program without designing it first. Let's try a bit of pseudo-code mixed with step-wise refinement. Here's what you said you needed:
        open a text file that has rows and colums (tab delimited), look for a name (in the first column), get the row that has the name so i can do stuff to it.
        Break that into separate lines and you have pseudo-code!
        open a text file (tab delimited) look for a name in the first column get the row that has the name so i can do stuff to it.
        That pretty much covers the task. Further refinement gets this:
        open a text file while I read a line from the file look for a name in the first column if the name is what I want, do stuff to the row end while
        Even more refinement:
        # still pseudo-code here open inputFile or die while ( read line from inputFile ) { get first item from tab-delimited line if ( first item equals "something I need" ) { do something important } } close file
        That's a pretty simple example. However, larger programs can be tackled in a similar fashion (though I use Warnier-Orr). There's probably nothing in the above pseudo-code that you can't figure out. By breaking it down that way, it's much easier to conceptualize and follow the logic.

        In fact, if you do have a problem with the above pseudo-code, I suspect that it's in the following line: "get first item from tab-delimited line". Now, rather than having a vague problem that you don't know how to approach (second quote above), you have a very specific task that is much easier to deal with.

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: hash/array
by Gloom (Monk) on Jan 26, 2001 at 01:33 UTC
    This sample of code show you ( I hope ) how to store a table in a hash of tables indexed by its first col.
    #!/usr/bin/perl -w use strict; my $data; # open file open FILE , "< /home/you/your.file"; # extract data and store it in a hash of tables while( <FILE> ) { @_ = split; $_ = shift @_; $data->{$_} = [@_]; } # close file ;) close( FILE ); # display result foreach( keys %{$data}) { print $_ , "\t" , join "\t" , @{$data->{$_}} , "\n"; }
    sorry if it's more confusing than helpfull but I don't see an easyer way to do that :)


    Update :

    Well, now i see clearer ...
    use enlightment; # ( thx clintp :)

      open FILE , "< /home/you/your.file";
      Bad form! When yer dealing with a newbie, it's best to show off those things in canonical form:
      open FILE, "/home/you/your.file" or die "Cannot open: $!";
      And as long as we're being picky, the extra indirection with the reference isn't needed. And that join gives an extra tab. And some more code-bumming... (UNTESTED!)
      #!/usr/bin/perl -w use strict; my %data; open FILE , "home/you/your.file" or die "Cannot open: $!"; # extract data and store it in a hash of tables while( <FILE> ) { @_=split; $data{$_[0]} = [ @_ ]; } close FILE; foreach( keys %data) { print join("\t" , @{$data{$_}}), "\n"; }
      There!
        What is good for the goose is good for the gander.

        You should do like perlstyle says and include all relevant information in the error message. In this case that includes the filename:

        my $file = "home/you/your.file"; open (FILE, "< $file") or die "Cannot read '$file': $!";
        If this appears within a function I would normally use Carp and then confess to the sad situation...