Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Unanswered: Control loop flow by using: next, last etc?

    Dear forums members:

    i am new to perl programming and taking a online class. Please give me some help, so I can lean from my mistake!

    I have this programming assignment from school. One requirement is,

    "Asks the user for the index (movie array index) of a movie to delete from the list. If the user enters invalid input, tell the user and prompt him again for a valid index. If the input is valid, delete that movie from the list."

    I used following code to handle this requirement:

    $input = <STDIN>;

    for($index = 0; $index <= $#movies; $index++)
    {

    if ($input == $index)
    {
    print "valid input.";
    last; #break out of the loop when input matched the index
    }

    #I want to use this elsif to handle when user entered
    # a valid index, but won't match exact index of the array.
    # 'next' will start another evalutaion of the for loop
    elsif($input != $index)
    {
    print "****"; #debugging statement
    print "\n";
    next;
    }

    # this else statement won't catch invalid input, due to incremment
    #of the index when index reach 2, for loop exits, and error message won't #show. The requirement is that loop structure won't exit until a valid index #is entered by user.
    else
    {
    print "Invalid index entered, please try again: ";
    $input = <STDIN>;

    }
    }

    -- I think i have a logic error here, please give me some suggestions to fix my problem. I appreciate your help!

    Jeff
    11-08-04

  2. #2
    Join Date
    Dec 2002
    Location
    Bathurst Australia
    Posts
    4
    Do your own homework!
    Don't you have a textbook?
    Go pickup a copy of the Perl Cookbook or something.

  3. #3
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Jeff,

    Perl is a funny language. An element of an array may or may not be defined. For example

    Code:
    #!\users\bin\perl
    use strict;
    use warnings;
    
    my @array=(1,,3); # note that there are two successive commas here
    ...
    leaves $array[1] as undefined. Moreover, you can test if something is undefined

    Code:
    ...
    if (defined ($array[1]) {
      print "\$array[1] is not defined\n";
       }
    else {
      print "\$array[1] is $array[1]\n";
       };
    In English, you're trying to code something that reads as:

    get index number;
    if there is a title for that index number then ....
    else ...

    so that becomes

    Code:
    my $index = <STDIN>;
    if (defined ($Titles[$index)) {
       } else {
       };
    since you're supposed to get more than one index number you can use a loop just like the one for reading titles

    Code:
    while (($_=<STDIN>) =~ /^.+$/) {
       if (defined ....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •