in reply to why won't is print every value???
I am by no means a monk, but being a full time Perl programmer, I know a lot of shortcuts. (Correct short cuts, that is, not hacks.) So, I kinda ramble over most of the code, I don't mean to step on toes or say the way you have it isn't right.. it's just a shorter way of doing the same thing. Like your argument code:
I would write it like this:my $num_of_params; $num_of_params = @ARGV; if ($num_of_params < 2) { die ("\n You haven't entered enough parameters !!\n\n"); }
Does the same thing, just easier to type. Next, you have:die "\nYou haven't entered enough parameters !!\n\n" unless $ARGV[1];
If this is for end-users, you are right in not being very descriptive with your error messages. However, especially during debugging, I would be a lot more loud and graphic with your errors, something like:open (INFILE, $ARGV[0]) or die "unable to open file"; open (OUTFILE, ">$ARGV[1]");
should work fine. Again, if it's for end-user use, turn off the errors once it's working if you need to. Next you have:open (INFILE, $ARGV[0]) or die "unable to open file $ARGV[0]: $!\n"; open (OUTFILE, ">$ARGV[1]") or warn "unable to open file $ARGV[1]: $!\ +n";
I think the post above me covered this, and probably already answered your main question. The reason that $self is is valid here is because the array is still valid. Each time you run the loop you are clearing the array, thus the one entry that you are displaying is probably the last entry in the file you read in, because the array isn't cleared at the end. Next you have:-snip- @array = (); @array = split (/\s+/, $line); -snip- # if i print $self here it prints out all of the results from that cat +ergory
Again, what you have works just fine, but since you are only reading in one character, why not just use 'getc'? Something like this:$choice = <STDIN>; # remove the newline character from the chosen option. chomp $choice; if ($choice eq 1)
Since getc only gets one character, no need for a chomp, and you save yourself a variable use. So, I hope that I got across the help I wanted to give without seeming to pick nits. Your code worked just fine (excepting the reason for the post of course), it's just that personally, I am always looking for a better way to do things, and in this case, I saw a few that I thought I would share.if (getc(STDIN) eq "1")
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: why won't is print every value???
by perlguy (Deacon) on Jun 05, 2002 at 20:33 UTC | |
by erasei (Pilgrim) on Jun 05, 2002 at 21:02 UTC |