Eri Mendz wrote:
> 
> Dear All,
> 
> I'm greatly overwhelmed by your quick help to my problem.
> Here's the corrected code:
> 
> #!/usr/bin/perl -w
> use strict;
> 
> # filename: reverse_string.pl
> # editor: # VIM - Vi IMproved 6.1
> # description: get user input and reverse input
> 
> print "Please enter any string, to quit press Ctrl-Z:\n";
> #im at work right now :-)
> chomp(my @input = <STDIN>);
> my $total_elements = scalar(@input);
> print "You have entered $total_elements arguments.\n";
> print "They are: \n";
> 
> foreach(1..$total_elements){
>     print "\t\[$_\] my $input[$_ - 1 ]\n";
> }

You could also write this as:

foreach ( 1 .. @input ) {
    print "\t[$_] my $input[ $_ - 1 ]\n";
}


> print "Press enter to see inputs in reverse order: ";
> my $press = <STDIN>;
> @input = reverse(@input);
> print "The reverse -> @input \n";
> 
> Just a quickie. Why did scalar $input in the foreach loop
> "works" without predeclaring it with my? I tried running both
> and it runs as expected. No scoping required for this control
> variable?

If you are referring to:

foreach ( 0 .. $#input ) {

you will notice that input starts with $# and not $ so it is NOT the
scalar $input.  If you have an array, say @input, then $#input contains
the value of the index of the last element in @input so that $array[
$#array ] and $array[ -1 ] and $array[ @array - 1 ] all refer to the
same array element.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to