473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Array Question

Stang02GT
1,208 Expert 1GB
I am learning some basic programming in linux ( Fedora 5). I am just creating some simple programs and I am wondering if anyone can help me figure this out.

I have this array....

Expand|Select|Wrap|Line Numbers
  1. let i=1
  2. for array3 in "$@"
  3. do
  4. names[i]="$array3"
  5. let i=i+1
  6. done
  7. for i in "${names[@]}"
  8. do
  9. echo $i
  10. done
How can i change this script to have the data read from within the program rather then from the command line?


thank you in advance for any help!
Oct 30 '07 #1
3 1331
Assuming recent version of bash (as you said FC5)
and a file in the following format:

Expand|Select|Wrap|Line Numbers
  1. entry1
  2. entry2
  3. entry3
  4.  
You can change the code like this:

Expand|Select|Wrap|Line Numbers
  1. while IFS= read -r; do
  2.     names[((++i))]="$REPLY"
  3. done<your_file_here
  4.  
  5. for i in "${names[@]}"; do
  6.     printf "%s\n" "$i"
  7. done
  8.  
  9. # or just: printf "%s\n" "${names[@]}"
Nov 4 '07 #2
Stang02GT
1,208 Expert 1GB
Thank you very much!!


Is there anyway you could help me understand what you did a little better?
Nov 5 '07 #3
Is there anyway you could help me understand what you did a little better?
First off I have to correct my own post (++i is sufficient, you don't need (())):
Expand|Select|Wrap|Line Numbers
  1. while IFS= read -r; do
  2.     names[++i]="$REPLY"
  3. done<your_file_here
IFS= and read -r

IFS= is "clearing" the internal field separator,
otherwise leading and trailing IFS characters will
be stripped: for example, a record like " word ... "
becomes "word ...". The -r option for read is necessary
to prevent some escape sequences from being expanded
- \n will not be expanded to a new line for example
(if that's not what you want,
use just while read;do ...).

names[++i]="$REPLY"

Some recent shells like bash, ksh93 and zsh
accept the syntax ++var and var++
for incrementing the value of a variable.
$REPLY is the default variable for some
statements like read and select.

HTH a bit.
Nov 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
8
by: T. Wintershoven | last post by:
Hello all, I have a form with some checkboxes. The names of these checkboxes come from an array. When i click the submit button the resultcode doesn't recognize the names when i want to check...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.