Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the voices are still...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (9)
    As of 2018-07-19 15:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (411 votes). Check out past polls.

      Notices?