in reply to Re: (Ovid - homework?) Re: hash/array
in thread hash/array

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.

Replies are listed 'Best First'.
Re: (Ovid - design your programs) Re(3): hash/array
by Yohimbe (Pilgrim) on Jan 26, 2001 at 01:20 UTC
    That is a very cool way to do it.
    But further: after your pseudocode is nearly done,
    =head1 PSEUDOCODE
    =cut
    around it. its too valuable to lose. By all means leave the comments in the code, but keep it all together as well. Sure it adds a couple lines, but will appreciate it big time, come maintenance day.
    --
    Jay "Yohimbe" Thorne, alpha geek for UserFriendly
Re: (Ovid - design your programs) Re(3): hash/array
by malaga (Pilgrim) on Jan 26, 2001 at 00:42 UTC
    thanks ovid. i actually did think about, and design my problem, but there is something about arrays that hasn't sunk in yet, so i get confused and trash it. thanks for the help.
Re: (Ovid - design your programs) Re(3): hash/array
by malaga (Pilgrim) on Jan 26, 2001 at 10:36 UTC
    i'm restating my problem, because i tried out each of the suggestions, and i couldn't get where i need to go with any of them. i am very much a beginner and am working hard on this, but spinning my wheels - i appreciate the help.

    i have a table like this: rowid course1 course2 course3 course4 marge psychology math engineering pe roy math reading art cooking etc. one of these fields will have lots of text with line breaks. i have a form. the form will send a request to a cgi, passing a value + which matches a value in "rowid" from the table. i need to open the file that contains the table, find the row that mat +ches the value passed by the form, read that row into an array (or ha +sh?) and print it out in html. i'm able to open the file and put it into an array and print it, but i + don't know enough perl yet to take just the row i want and assign la +bels to each value.
      Okay, lets say your array is like this (Supersplit is the simplest way to create it)
      $ary[0][0]='marge'; $ary[0][1]='phychology'; $ary[0][2]='math'; $ary[1][0]='roy'; # etc...
      Then you want to search the first column of that array for the desired rowid, and print it out in an html table.
      my $rowid = $q->param('rowid'); # get the value from our CGI.pm objec +t # search the first column for the rowid, and store the first match in +$row_ref my ($row_ref) = grep { $_->[0] eq $rowid } @ary; # output an html table containing column head labels and a row of data print '<table>', '<tr><td>rowid</td><td>course1</td><td>course2</td></tr>', '<tr>', (map {"<td>$_</td>"} @$row_ref), '</tr>', '</table>';
      Hope that helps in some kind of way, I'm not sure what else you want.