Eri Mendez wrote:
> hi all,
> 
> another newbie here. im having problem how to print to stdout the
> string[s] entered by user and appending a subscript integer after that
> string. this is my modification to exercise 1 chapter 3 of Llama book:
> 
> #!/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-D:\n"; 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\[", $_, "\] $_\n";
> }
> 
> ...
>
> my target is print the strings after the subscripts. I tried several
> variations but cant get it right. if i uncomment the 2nd foreach
> block in place of first, i get the strings but again in dupes. grrrr,
> i feel im close to the solution. is split again needed here? just
> give me hint, not straight answer so i can pound my head to think.
> TIA. 

I see you've already gotten the "straight answer", but:

Hint #1: array subscripts start at 0, unless you monkey with $[ (but don't
do that).

Hint #2: This line:

   print "\t\[", $_, "\] $_\n";

prints the value of $_ twice. You never print the value from the array
@input. Think: how to do that?

Also, you do not need to escape the [ and ] characters _unless_ you do not
want perl to see them as array subscripts.  So:

   print "$foo[0]"    -> prints value of first element from array @foo

but

   print "$foo\[0]"   -> prints value of scalar $foo, followed by "[0]"
chars

The way you're printing "[" and "]" do not need to be escaped, because they
don't look like array subscript brackets in the context.

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

Reply via email to