Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Auto Fill

by PilotinControl (Pilgrim)
on May 15, 2015 at 20:00 UTC ( [id://1126810]=perlquestion: print w/replies, xml ) Need Help??

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

Good Afternoon Monks!
I am trying to accomplish the following in a more automated fashion. My code is below and here is the question: Based on if the truck is filled with which ever commodity I would like the second field to be automatically filled in with either being loaded or emptied.

print "==============\n\n"; print "| COMMODITY: |\n\n"; print "==============\n\n"; my $cmmdty = <STDIN>; $cmmdty = <STDIN> until defined $cmmdty; chomp $cmmdty; cls(); print "====================\n\n"; print "| LOADED OR EMPTY: |\n\n"; print "====================\n\n"; my $lore = <STDIN>; $lore = <STDIN> until defined $lore; chomp $lore; cls();

I know I need an if statement like below:

if ($cmmdty eq 'NONE') { print "E"\n"; } elsif { print "L"\n; }

Am I on the right track? I just need to be guided on to where to put the code to get the desired output I am looking for. Thanks in advance!

Replies are listed 'Best First'.
Re: Auto Fill - LAZY question!
by ww (Archbishop) on May 15, 2015 at 20:24 UTC

    Taking the parts of your guidance request in reverse order:

    "to get the desired output I am looking for"

    Is printing a simple "E" or "L" your "desired output?" If so, do you nonetheless think the second snippet is somehow deficient? If so, how?

    "I just need to be guided on to where to put the code.... "

    Probably it would be best to put the second snippet into the first, after the input from <STDIN> for $cmmdty has run its course.

    But what's the point of the second input routine? What is expected when asking user to enter a $lore? Does a $lore have to do with your question?

    And at least as much to the point, how much thought did you put into solving your (trivial) question before posting? Why did you bother posting instead of trying your possibilities... or reading the applicable tuts?

    Paraphrasing numerous suggestions found in the FAQs and elsewhere here in the Monastery, we really do like to see some genuine effort. That fact that you posted some code hardly obscures the fact that there's very little sign you made any serious effort to answer your actual question.

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

      The code provided is what is currently being used...manually entering in if the truck is loaded or empty...for example: a screen pops up asking what the commodity is say Scrap: then the next screen would automatically be filled with L for being loaded. If the Commodity screen pops up and NONE: is typed then the next screen would automatically show E for being empty.

Re: Auto Fill
by NetWallah (Canon) on May 15, 2015 at 20:47 UTC
    If I understood your question correctly,
    you are looking for Pre-populating <STDIN> with a default value.

    Also - in your code, you may want to change "elsif" to plain "else".

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

      Pre-populate based on input: if input is the word NONE then the prepopulated output for the next screen would be E and if any other input the prepopulated output would be L

        Did you notice that my previous post had a Link to information on how to implement this "pre-population", along with a discussion on why it was not a good idea ?

                "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: Auto Fill
by golux (Chaplain) on May 16, 2015 at 14:36 UTC
    Just one suggestion about your code. When I see something duplicated like this:
    print "==============\n\n"; print "| COMMODITY: |\n\n"; print "==============\n\n"; my $cmmdty = <STDIN>; $cmmdty = <STDIN> until defined $cmmdty; chomp $cmmdty; cls(); print "====================\n\n"; print "| LOADED OR EMPTY: |\n\n"; print "====================\n\n"; my $lore = <STDIN>; $lore = <STDIN> until defined $lore; chomp $lore; cls();
    it immediately makes me want to encapsulate the duplicated code into a subroutine, so as to follow the doctrine of DRY.

    Here's one way you could rewrite it:

    sub prompt_for_value { my ($label) = @_; my $len = length($label); my $line = "=" x ($len + 5); # Display the prompt, eg. # # print "==============\n\n"; # print "| COMMODITY: |\n\n"; # print "==============\n\n"; print "$line\n\n"; print "| $label: |\n\n"; print "$line\n\n"; my $value = ""; while (1) { chomp(my $value = <STDIN>); if ($value) { cls(); # Is this subroutine defined? return $value; } } } my $cmmdty = prompt_for_value("COMMODITY"); my $lore = prompt_for_value("LOADED OR EMPTY");
    If you do this, you've got a subroutine "prompt_for_value()" which you can use for other input. If there's a bug in your subroutine, or you want to make an improvement in it (like changing "\n\n" to a single newline, for instance), you only have to make the change once within the subroutine and all callers of the subroutine will now do the same thing.

    Note that the prompt takes the length of the label into account, so the containing box is the right size.

    This kind of code reuse will really help your programming skills.

    say  substr+lc crypt(qw $i3 SI$),4,5
Re: Auto Fill - FIXED
by PilotinControl (Pilgrim) on May 15, 2015 at 23:38 UTC

    Hello Monks,
    After stepping back from the script I finally figured it out on my own...sometimes you just have to step back and re think what it is you are doing...fixed code below:

    print "==============\n\n"; print "| COMMODITY: |\n\n"; print "==============\n\n"; my $cmmdty = <STDIN>; $cmmdty = <STDIN> until defined $cmmdty; chomp $cmmdty; if ($cmmdty) { if ($cmmdty eq "NONE") { chomp(my $lore = "E"); } else { chomp(my $lore = "L"); cls();

    It does exactly as I expect it to do.

      "It does exactly as I expect it to do."

      What did you expect this code to do?

      if ($cmmdty eq "NONE") { chomp(my $lore = "E"); } ...

      Here's what it does:

      • creates a new lexical variable called $lore
      • assigns the string 'E' to $lore
      • calls chomp which does absolutely nothing
      • allows $lore to go out of scope so that it cannot be accessed by any other part of your code

      In short, create a variable, assign a value, perform a pointless operation and then throw it all away.

      You possibly have the same problem with 'chomp(my $lore = "L");' but that part of the code is incomplete so I can't tell.

      -- Ken

      It does exactly as I expect it to do.
      Are you sure ?

      How will you access the value of $lore , after this part of your script completes ?

      And, you are missing a closing brace.

              "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

        The closing brackets (2) have been added and the value of $lore gets printed to a flatfile database for later retrieval.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-28 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found