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

Hello monks, do we evaluate pseudocode for certain problem here? if yes i have written a pseudocode and i will post it here so that you guys can evaluate if it is a right way for pseudocoding or not?

Replies are listed 'Best First'.
Re: pseudocode for getting data from table
by GrandFather (Saint) on Sep 22, 2020 at 06:07 UTC

    We have a fairly narrow Perl focus here, but can cope with related topics. If something isn't really a Perl question (like: "How do I choose a web host") it is generally accepted if marked [OT] (Off Topic).

    That said, if you are putting together some pseudo code to figure out how you might write a Perl script then I think that is entirely within the usual remit of the site - go for it!

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Thank you so much for supporting so basically i am writing a pseudocode for the following problem Given a table as shown in the example below, print out the gene ID as well as the expression level for the gene, if the expression level is >= 100. The pseudo code must be generic, so that it can be used on another, but similar, matrix.

      Type Gene ID Organism Expression l +evel Transcription factor Q0V7X4 Arabidopsis thaliana 58 Transcription factor P52655 Homo sapiens 2107 Protein kinase P42753 Arabidopsis thaliana 11 Protein kinase Q8ND76 Homo sapiens 987765

      Here is the pseudocode that i have written to acheive this task please have a look if i write the pseudocode correctly or does it need editing?

      table <- INPUT TABLE WHILE line IN table IF line >= 100 expressionLevel <- line PRINT geneID IN table PRINT expression level

        That is under-specified. What is the table layout? Spaces only or tabs? With TAB's, have a loog at Text::CSV and it's faster parent Text::CSV_XS. If spaces only, you have a problem, as the first line is not aligned with the rest. If it were, pack/unpack would most likely be of help:

        # Example using split on multiple spaces open my $fh, "<", "INPUT"; while (<$fh>) { $. < 100 and next; chomp; my ($typ, $gid, $org, $lvl) = split m/\s\s+/ => $_; say "$. $gid, $lvl"; } close $fh; # Example with TAB's use Text::CSV_XS; open my $fh, "<", "INPUT"; my $csv = Text::CSV_XS->new ({ binary => 1, sep_char => "\t", auto_dia +g => 1 }); while (my $line = $csv->getline ($fh)) { $csv->record_number < 100 and next; my ($typ, $gid, $org, $lvl) = @{$line}; say "$. $gid, $lvl"; } close $fh; # Example with unpack (I manually aligned the header line) open my $fh, "<", "INPUT"; while (<$fh>) { $. < 100 and next; chomp; my ($typ, $gid, $org, $lvl) = unpack "A25 A10 A26 A*" => $_; say "$. $gid, $lvl"; } cloase $fh;

        Enjoy, Have FUN! H.Merijn
        I see a problem here:
        IF line >= 100
        You first need to extract the expression level from the line, then compare it to 100. You can't compare the whole line against 100, as it's a string.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        What do you need pseudocode for? If your employer requires it to document program design before you begin code, he probably specifies a grammar and has his own idea about what is sufficient detail. At the other extreme, it can be something that you write on your napkin while enjoying your morning coffee. In the first case, we can only guess what the boss might want. As long as it is for your own use, you are the best one to decide when it is done. It is done when you believe that the work required to improve it is greater than the work it will save you later.

        I often use Perl itself as pseudocode. I call subroutines which do not exist yet, omit initialization and formats, make my best guess at syntax that I am not sure of, etc. Then I compile it and fix the syntax errors. Then I add details until I have a working program.


        Like BillKSmith I tend to go straight to sketch code rather than something that looks a little like code but doesn't morph directly into anything in particular. That gives me a head start in writing the actual code and also helps me think about testing.

        So for your sample I might write something like:

        use strict; use warnings; my @tableLines = FetchTable(); for my $tableLine (@tableLines) { next if $tableLine->{expressionLevel} >= 100; print "TableLine->{geneID} TableLine->{expressionLevel}\n"; }

        which is at least as good as the pseudo code at elaborating the idea for the program, is insignificantly harder to set down, and focuses attention on important elements of the program that are unclear in the pseudo code (what does PRINT geneID IN table really mean?). In the real world these days tools like pseudo code and flow diagrams (for code) are seldom used and are of limited utility compared to things like test driven development.

        If this is a homework exercise it may still be worth sketching the program as I have done above and then "reverse engineer" the pseudo code from it. If my sketch code captures your intent then the pseduo code you have as it stands is fine.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: pseudocode for getting data from table
by perlfan (Vicar) on Sep 23, 2020 at 15:07 UTC
    Sometimes I hand write pseudocode, but I call it an "algorithm". Then I implement the algorithm in Perl and run/debug/write until it works. I don't focus on optimiziation if it's outside my normal idiomatic domain. I am telling you this because I think it's most efficient for all of us, but you especially you, to come here during the phase where maybe you've run into an issue with your real code. e.g., "I can't get this to work" or "I don't know how to do this part", etc.

    Back to your question. What "table" do you mean? database, html, excel, csv? In all of those cases there is a Perl module on CPAN that will help you. In the case of it being your kitchen table, some monks know a little about wood working as well xD