Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with simple perl program with array

Newbie
 
Join Date: Jan 2006
Posts: 1
#1: Jan 25 '06
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:

Newbie
 
Join Date: Jul 2006
Posts: 11
#2: Jul 10 '06

re: Problem with simple perl program with array


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
Newbie
 
Join Date: Jul 2006
Posts: 3
#3: Jul 14 '06

re: Problem with simple perl program with array


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]");
Newbie
 
Join Date: Jul 2006
Posts: 3
#4: Jul 14 '06

re: Problem with simple perl program with array


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
:)
Reply