The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regarding interactive ID of batch job pradeep.edara UNIX for Dummies Questions & Answers 1 12-20-2007 10:49 AM
Interactive menu Klashxx Shell Programming and Scripting 0 12-11-2007 10:03 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 08:42 AM
Interactive Mode? Nomad UNIX for Dummies Questions & Answers 2 08-16-2002 06:59 AM
Interactive String odogbolu98 Shell Programming and Scripting 3 06-20-2002 01:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-25-2008
gecko2424 gecko2424 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
Interactive Array Menu

This should be simple, but i haven't done it before...

KSH

I am reading a file into an array and currently displaying the values to the screen. What I need to do is to display a subset of those values, likely numbered, and prompt the user to select one. When they enter the number, it retrieves the assoicated array value and outputs it to the screen. (this will be used for more complex functions later)

For example, the program is run and the first 10 rows are displayed. I select a number or hit a character to proceed to the next page. This would dump the first 10 rows and load the next 10 (into another array?) I select a value and the row associated with that value echoes to the screen.

Any suggestions are greatly appreciated.
My code so far:
Code:
#!/bin/ksh
 
# define array
set -A line_array
# select input file
file_name="$scripts/cb_list.dat"
 
# populate the array
i=0
while read file_line
do
line_array[i]=${file_line}
let i=${i}+1
done < ${file_name}
 
i=0
while [ ${i} -le ${#line_array[*]} ]
do 
echo ${line_array[i]}
let i=$i+1
done
# end of variable_define.sh


Last edited by gecko2424; 07-25-2008 at 09:48 AM..
  #2 (permalink)  
Old 07-28-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
No need to 'dump' the values since you already have all of them loaded in the array, you can just calculate the current page offset, (say 10 for the second page), and just display items 11-20 in the array for that page.

Your code looks fine so far, what part are you having difficulty with?
  #3 (permalink)  
Old 07-31-2008
gecko2424 gecko2424 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 2
EDIT:
I have it working now, code is below for anyone that it might help. I did what i needed with a nested function.

Code:
#!/bin/ksh
# define array
set -A line_array

# select input file
file_name="cb_list.dat"


# populate the array
i=1

while read file_line
do
   line_array[i]=${file_line}
   let i=${i}+1
done < ${file_name}


#Display selection options
i=1
x=10

var_select ()
{
while [ ${i} -le ${x} ] && [ ${i} -lt ${#line_array[*]} ]
do
 echo
 echo $i ${line_array[i]}
  let i=$i+1
done

#Select an option

    echo
    echo "Select an option number or press enter: \c"
    read num
disp_select
}


disp_select ()
{
if [ ${num} -le ${i} ]
  then
    echo "You have selected: ${line_array[num]}"
else
    x=${x}+10
    var_select
fi
}

var_select


# end

Last edited by gecko2424; 07-31-2008 at 01:41 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0