Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: pseudocode for getting data from table

by GrandFather (Saint)
on Sep 22, 2020 at 06:07 UTC ( [id://11122057]=note: print w/replies, xml ) Need Help??


in reply to pseudocode for getting data from table

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

Replies are listed 'Best First'.
Re^2: pseudocode for getting data from table
by shabird (Sexton) on Sep 22, 2020 at 06:41 UTC

    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

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

      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.

      Bill

      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://11122057]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found