Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

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

Util meditates on:
"If I put the whole thing into the loop i end up asking the same question over and over to the user."


Yes. This will anger your users.

Where should Anonymous Monk request the user's input? Before, during, or after the while(<INFILE>){} loop?
For clarity below, I will set aside strict, /pattern/oi, chomp, and similar elements of our craft.

During:
while (<INFILE>) { $word = <STDIN>; print if /$word/; } # Officiate is asked for input on every line of the file. # Officiate throttles monk; blacking out, you meditate on your error.
After:
while (<INFILE>) { $line = $_; } my $word = <STDIN>; print $line if $line =~ /$word/; # $line only contains the last line of the file! # Officiate makes bad decision based on wrong output. # Monk goes hungry.
After (fixed):
while (<INFILE>) { push @lines, $_; } $word = <STDIN>; foreach (@lines) { print if /$word/; } # File data is stored until $word is known. # Officiate gets correct output, is pleased while file is small. # Monk is thrashed when the disk thrashes on a large file.
Before:
$word = <STDIN>; while (<INFILE>) { print if /$word/; } # There is no need to store the file data. # Harmony is achieved.

Util comes out of Monk Funk, enters Homework Mode
Practical notes on Anonymous Monk's posted code:

  • The first field in an array is $array[0], not $array[1].
  • eq is for string comparisons. Use == for numbers.
  • $self="something\n"; print "$self\n"; yields two newlines per line.

FWIW, here is how I would do this sort of thing in the real world. No huge files, @types dynamically calculated. Code tested.

#!/usr/bin/perl -W use warnings 'all'; use strict; # Scans the input file(s), breaking each line into fields. # Builds a key from prettified $type, and stores an array of # output lines in a hash under the key. Prints a list of all # the types seen in the file(s), and asks the user to choose one. # Prints the output lines stored for that key/type. my $limit = 30; my $usage = "Usage: $0 infile\n"; die $usage unless @ARGV; sub pretty { return ucfirst lc shift } my %lines; while (<>) { my ($whatever, $type, $num) = split; my $key = pretty($type); push @{ $lines{$key} }, "$type\t$num\n" if $num < $limit; } my @types = sort keys %lines; my $ask = "Please select a category:\n"; $ask .= "\t$_. $types[$_]\n" foreach 0 .. $#types; my $type; while (not $type) { print $ask; chomp(my $choice = <STDIN>); $type = $types[$choice] or print "$choice was not an option!\n" and +next; } my $key = pretty($type); print @{ $lines{$key} };

Clearly, Util is suffering from Perl Golf induced dementia.
Waits to get slammed with "Deprecated use of 'officiate'" error.


In reply to Re: why won't is print every value??? by Util
in thread why won't is print every value??? by Anonymous Monk

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 lurking in the Monastery: (9)
As of 2024-04-19 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found