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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Well, since you've exposed your dirty little secret, and that takes courage, I'm going to join you: I too follow NASCAR. Go #20.

Anyway, here's a quick and dirty fix. Note the first two lines after !#. That's the most important part. The second most important part, and the part that fixes your problem, is chomping the input BEFORE looking at it to determine whether you want to use the default file. Before you chomp the input, it has a newline character in it, which resolves to true if you test it. If the person only hit enter, and you chomp the value, THEN you have a value that will test false.

Note there are lots of other ways to improve your code and this is "quick and dirty" at best.

#! /your/path/to/perl use strict; use warnings; print "\n"; print "Which file do you want to open? "; # we are asking which file+ +to open my $file0 = <STDIN>; chomp($file0); my $file = $file0 || "somelist"; #here's my effort to apply a defaul +t t+o the input if it is blank. # THIS IS WHAT I WOULD LIKE TO DO -> if stdin is blank, i'd like to as ++sign $file a variable. print "OK, I will look in $file.\n"; # print "\n"; print "Which driver do you want to see? "; # we are asking to output+ + info from a particular line chomp(my $name = <STDIN>); # the more idiomatic way print "Aah, $name, truly a great driver.\n"; # Our requested file is accessed and displayed my $bibfile = "$file"; open(IN, "<$bibfile") or die "Can't open $bibfile\n"; print "File Contents:\n"; while (my $line = <IN>) { chomp($line); my ($Drivers,$Points_Rank,$TotalPts,$Behind,$Starts,$Poles,$Wins,$To +p5,$Top10,$DNF,$Winnings) = split /\,/, $line; if ($Drivers=~ m/$name/i) { print " $Drivers, $Points_Rank, $Top10, $Wins, $Starts, $DNF \n"; } # } close IN;

I like computer programming because it's like Legos for the mind.

In reply to Re: Adding Default Values [for BEGINNERS] by OfficeLinebacker
in thread Adding Default Values [for BEGINNERS] by brusimm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-16 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found