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

    Unanswered: How to use loop strcture to read command line input

    Dear forums members:

    I have this lab exercise which requires user to input movies titles
    from command line (one title perline)
    e.g. Friday Night Light(enter)
    Open Range(enter)
    ...

    I need a loop structure to take moive titles from command line,
    until user hit enter(without typing a tile) the loop will be exited.

    i think a while loop will do the job. But I don't know what condition
    should be in the while loop to catch the 'enter key' entered by user.

    --movie titles entered by user will be added to the original
    movie list(i think use a array to store the new titles, then use join
    or push function to merge two arrays, just my thought not sure if it works)

    e.g. while(?)
    {
    store new titles in array.
    }

    Could someone give me some gueestion or hints, so I can complete this lab
    exercise. Thanks!

    Jeff
    11-08-04

  2. #2
    Join Date
    Dec 2002
    Location
    Bathurst Australia
    Posts
    4
    You're asking someone to help you do your homework?
    You're at school/college/university to learn, try it some day.

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

    Code:
    #!\users\bin\perl
    
    use strict;
    use warnings;
    
    my(@a_Titles);
    while (($_=<STDIN>) =~ /^.+$/) { # "loop" only if the title IS NOT empty ie ""
      chomp;
      push(@a_Titles,$_);
      # print array index and title
      #print "$#a_Titles: $a_Titles[-1]\n";
       };
    # print titles separated by lines
    #print join("\n",@a_Titles);

Posting Permissions

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