Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi, i am writing a script that sort elements of an array into certain criteria, and then asks the user what catergory they want to see. I have given a snippet of code here, my problem is that i want to print out every value of a certain criteria when the user asks to see it, however my print statement only prints a single value. Am i not passing the whole array into the scalar variable?? is it because it isn't nested in the loop correctly?? - if i put the whole thing into the loop i end up asking the same question over and over to the user. what am i doing wrong??
#! /usr/local/bin/perl -w use strict; my $num_of_params; $num_of_params = @ARGV; if ($num_of_params < 2) { die ("\n You haven't entered enough parameters !!\n\n"); } open (INFILE, $ARGV[0]) or die "unable to open file"; open (OUTFILE, ">$ARGV[1]"); my $line; my @array; my $self=0; my $choice; while (<INFILE>) { $line = $_; chomp ($line); @array = (); @array = split (/\s+/, $line); if (($array[1] =~ /^Happy/) && ($array[2] <30)) { ; $self = "$array[1]\t$array[2]\n"; # if i print $self here it prints out all of the results from that cat +ergory } print STDOUT "Please select the catergory of results that you wish t +o see;\n\n\t 1. Hello\n\t2.Happy \n\t3. unhappy \n\t4.grumpy \n\n"; $choice = <STDIN>; # remove the newline character from the chosen option. chomp $choice; if ($choice eq 1) { # if i print $self here it only prints one result, not the entire list print "$self\n"; } close OUTFILE;

In reply to 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: (5)
As of 2024-04-24 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found