Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: pseudocode for getting data from table

by shabird (Sexton)
on Sep 22, 2020 at 06:41 UTC ( [id://11122060]=note: print w/replies, xml ) Need Help??


in reply to Re: pseudocode for getting data from table
in thread pseudocode for getting data from table

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

Replies are listed 'Best First'.
Re^3: pseudocode for getting data from table
by Tux (Canon) on Sep 22, 2020 at 07:09 UTC

    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

      You have wrote an implementation of it but i asked to check my pseudocode if it is correct or not?

        (pseudo)code cannot be validated if the (input)requirements are not clear.

        So, sorry if I took the fun part out of your assignment, but this is how I try to make sense of pseudo-code.

        What pseudo-code also doesn't clearly define is the output-format and validation of input. Also things to think about (also in pseudo-code).

        As I personally "do" a lot of perl, I think perl, so my pseudo-code *is* perl. Be it with typoes and syntax errors, but perl (when using strict and warnings) will point me at those for me to fix.


        Enjoy, Have FUN! H.Merijn
Re^3: pseudocode for getting data from table
by choroba (Cardinal) on Sep 22, 2020 at 12:38 UTC
    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]
Re^3: pseudocode for getting data from table
by BillKSmith (Monsignor) on Sep 22, 2020 at 14:36 UTC
    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.

    Bill
Re^3: pseudocode for getting data from table
by GrandFather (Saint) on Sep 22, 2020 at 21:18 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122060]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-18 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found