Problem with simple perl program with array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xedsmr
    New Member
    • Jan 2006
    • 1

    Problem with simple perl program with array

    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:
    Code:
     
    #! perl -w 
    $inc = 0;
    while ($inc < 4) { 
    	if($inc < 3){ 
    		print 'Enter some text: '; 
    		chomp($input = <STDIN>); 
    		@array = $input;
    	}else{ 
    		print 'Enter a number: '; 
    		chomp($input = <STDIN>); 
    	} 
    	$inc++;
    } 
    printf("The text in line $input is: @array");
    C:\Perl\bin>per l 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:
    Last edited by Niheel; Jan 25 '06, 05:21 AM.
  • jagannathan
    New Member
    • Jul 2006
    • 11

    #2
    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

    A lesson with examples of Array indexing, Number Arrays, and complete array manipulation in PERL. Learn everything about PERL Arrays.

    Comment

    • byadwad
      New Member
      • Jul 2006
      • 3

      #3
      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]");

      Comment

      • byadwad
        New Member
        • Jul 2006
        • 3

        #4
        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
        :)

        Comment

        Working...