Find the answer to your Linux question:
Results 1 to 6 of 6
I am trying to create an array that will allow me to print a list after selection such as: 1) Location A 2) Location B 3) Location C ... Please ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4

    Programming Question


    I am trying to create an array that will allow me to print a list after selection such as:

    1) Location A
    2) Location B
    3) Location C
    ...

    Please select your locations:

    Would you like to print selected locations?

    Print

    I have no idea how to approach this problem and don't know whether Perl, Bash or shell script would be the most useful in this particular situation. Help would be very much appreciated - Thank you in advance.

  2. #2
    Just Joined!
    Join Date
    Dec 2009
    Location
    Maryland, USA
    Posts
    82
    Do you have any experience with any programming language? Which one, how much?

    How much data is associated with each location? Is it all text? In other words, what will the program print in response to the user's selection(s)?

    How is the location data loaded? Is that part of the program too, or is it contained in a file that is created/edited with another program?

    Do you care if the program is graphical or command line? Which would you prefer?

    Could what you want be accomplished with web pages containing the necessary data linked to the menu items? That would be an easy approach for a non-programmer using a word processor, HTML editor, personal Wiki, etc.

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    4
    I have little experience with programming - mostly perl and bash. It is all text and little data is associated with each location (2-3 words). The program will reprint a selection of items chosen from a pool of all the locations which would diplayed. Command line would be more beneficial to me, but graphical interface I imagine would most likely be easier. I'm trying to create an executable script.

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Location
    Maryland, USA
    Posts
    82
    My advice is to go with what you know, unless this is an exercise in learning a new language. Since there's little data associated with each choice, it seems either of your familiar languages would be fine. It's also fine that you're satisfied with a command line interface. Learning how to do it in a GUI may be more trouble than it's worth.

    You should be able to find an example of a bash script that does what you want that follows the basic formula:

    present a menu of choices
    get the user's choice
    react to the user's choice

    I have a .pdf document called "Advanced-Bash Scripting Guide" by Mendel Cooper that you should be able to find with Google. Example 10-25 in that guide might be a good starting point. I haven't explored Perl so can't help there, but the logic and flow control concepts should be similar.

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    15
    My advice is also the same as Greg; You try with what you know. Since your problem relates with arrays and text i think perl will be the most suitable. Its a great programming language for newbies. You can start from perl documentation which is available in most of the linux distributions. And dont forget Google.

    Wish you fun with programming

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    645
    Code:
    #!/bin/bash
    
    PS3="Would you like to print selected locations" "
    select location in "Location A" "Location B" "Location C"; do
       [ "${location}" != "" ] && break
    done
    echo "You selected location: ${location}"
    or using an array

    Code:
    #!/bin/bash
    
    my_array=("Location A" "Location B" "Location C")
    
    PS3="Would you like to print selected locations" "
    select location in "my_array[@]}"; do
       [ "${location}" != "" ] && break
    done
    echo "You selected location: ${location}"

Posting Permissions

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