469,327 Members | 1,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Problem with simple perl program with array

1
Here is my perl program followed by my output.

What am I missing? Thanks so much for any help

Write a Perl program that reads a list of strings and then a number (all on separate lines), and then prints one of the lines from the list as selected by the number.
Here’s what it should look like when you run this program:

% program.pl
Enter some text: This is the first line
Enter some text: This is the second line
Enter some text:
Enter a number: 2
The text in line 2 is: “This is the second line”
%

This is my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #! perl -w 
  3. $inc = 0;
  4. while ($inc < 4) { 
  5.     if($inc < 3){ 
  6.         print 'Enter some text: '; 
  7.         chomp($input = <STDIN>); 
  8.         @array = $input;
  9.     }else{ 
  10.         print 'Enter a number: '; 
  11.         chomp($input = <STDIN>); 
  12.     } 
  13.     $inc++;
  14. printf("The text in line $input is: @array"); 
  15.  
C:\Perl\bin>perl program.pl
Enter some text: This is the first line
Enter some text: This is the second line
Enter some text:
Enter a number: 2

The text in line 2 is:
Jan 25 '06 #1
3 8647
hi there
check this sites
brillant listin to use arrays

if u use this code
@array = $input;
might not work try

using "push" as given in the link

as u can see the array is store only the last entry

http://www.tizag.com/perlT/perlarrays.php
Jul 10 '06 #2
Please try this!



#!/usr/bin/perl -w
$inc = 0;
while ($inc < 4) {
if($inc < 3){
print 'Enter some text: ';
chomp(my $input = <STDIN>);
push (@array, $input);
}else{
print 'Enter a number: ';
chomp($number = <STDIN>);
}
$inc++;
}

print "Array - @array\n";
printf("The text in line $number is: $array[$number-1]");
Jul 14 '06 #3
Please try this!
I have made this program generic ............. Yet to take care of error handling ........

################################################## #######
#**************Auther : Basavaraj Yadwad*********************************
#Dated on : 14/7/2006********************************************** ********
################################################## #######

#!/usr/bin/perl -w
system "clear";
$inc = 0;
print "Enter number of strings u wish ........\n";
$strnum=<STDIN>;
chomp($strnum);
while ($inc <= $strnum) {
if($inc < $strnum){
print 'Enter some text: ';
chomp(my $input = <STDIN>);
push (@array, $input);
}else{
print 'Enter a number: ';
chomp($number = <STDIN>);
}
$inc++;
}
#To display the array elements each on seperate line!
print "The elemetnts you entered are as follows:\n";
$serial=1; #This is to add lines on console
foreach $element(@array)
{print "$serial. $element\n";
$serial++;
}
#Your desired output follows here!
printf("The text in line $number is: $array[$number-1]\n");
################################################## ########

Have a great day!

Cheers!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

-Basu
:)
Jul 14 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by slash | last post: by
3 posts views Thread by lonelyplanet999 | last post: by
reply views Thread by dohnut | last post: by
9 posts views Thread by Martin Foster | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
2 posts views Thread by kelly | last post: by
223 posts views Thread by Pilcrow | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.