Sponsored Content
Homework and Emergencies Homework & Coursework Questions i have no understanding of how to write an array or use one, please help! Post 302463298 by jim mcnamara on Saturday 16th of October 2010 09:26:15 PM
Old 10-16-2010
1. declare -a [arrayvariablename] is optional, but good practice
So:
Code:
declare -a myarr
# we now have an empty array called myarr

2. to assign a value to an array element (one of the members of the array:
Code:
myarr[13]=4

This now means that the fourteenth (remember we chose to start counting from 0?)
contains 4.

3. So we want to enter ten numbers right? We need a loop to count from 0 .. 9
Code:
for i in 1 2 3 4 5 6 7 8 9
do
  # put your code here
done

i becomes 0 then 1,2,3..... as the loop goes on.

Code:
for i in 0 1 2 3 4 5 6 7 8 9
do
    echo -n 'Enter a number: '
    read myarr[$i]   # read lets you type in a number then assign it to a variable
done

You now have 10 array element, each of which has a number in it. Let's find the biggest one.
Code:
echo ${myarr[4]}  # NOTE the ${} part -when you want to get the value in an array element

So let us stomp thru the array and assign something to another variable, call it max
Code:
max=-2147483647  # I picked a negative number because your prof will do that to test 
# your code
# poor profs love to to go beyond what they tell you.... they think it somehow teaches (IMO)  

for i in 0 1 2 3 4 5 6 7 8 9
do
   [  $max -lt ${myarr[i] ] && max=${myarr[i]}
done

display the result:
Code:
echo "maximum value is $max"

Have fun....

PS:
Code:
[ $max -lt ${myarr[i] ] && max=${myarr}

is shell shorthand for if [ ] then do something endif.
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Understanding read/write and kernel interaction

Ok, so I'm trying to finalize my understanding of read/write and kernel interaction. read(): You have a library function that has as it's first parameter (what the open file to read from is), second parameter( a pointer to a buffer (is this the location of a buffer in the user area or the... (1 Reply)
Discussion started by: Krothos
1 Replies

2. Programming

Understanding read/write and kernel interaction

Ok, so I'm trying to finalize my understanding of read/write and kernel interaction. read(): You have a library function that has as it's first parameter (what the open file to read from is), second parameter( a pointer to a buffer (is this the location of a buffer in the user area or the... (7 Replies)
Discussion started by: Krothos
7 Replies

3. Shell Programming and Scripting

Understanding array in array

Hello, I have an array in which output of a query is stored. These values can be accessed by, foreach my $val (@result){ print ("\nValue : @{$val} from result");} Can anybody please explain me, how to delete any particular element in array @result ? (4 Replies)
Discussion started by: gentleDean
4 Replies

4. Shell Programming and Scripting

Push records to array during implicit loop and write to file

NEWBIE ALERT! Hi, I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.... (0 Replies)
Discussion started by: jospan
0 Replies

5. Shell Programming and Scripting

i have no understanding of how to write an array or use one, please help!

im in a basic unix class and our professor speaks broken engliash so i can never understand what exactly we are doing in class and i have no prior experience with unix. we were given an assignment to make 2 files. one to Input 10 numbers and print out the biggest number, and one to Write a script... (1 Reply)
Discussion started by: wendyshephard
1 Replies

6. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

8. Shell Programming and Scripting

Write filenames into an array variable

Hello there, I am a newbie to unix and i have been trying to code a shell script for bash shell which reads the files in a folder using ls and writes them into an array variable. But, i am running into several errors while running the script. The script is given below: #!/bin/bash... (16 Replies)
Discussion started by: amrutha0303
16 Replies

9. Shell Programming and Scripting

Grab data between 2 keywords any do an array operation and write the file intact

Hi Unix Gurus, I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword. for eg: I need to search between Test = 000; and Test = 000; and find K9 and replace with M9 INPUT FILE Define { Replace = K9; Test =... (6 Replies)
Discussion started by: naveen@
6 Replies
dla_gerpvgrw.f(3)						      LAPACK							 dla_gerpvgrw.f(3)

NAME
dla_gerpvgrw.f - SYNOPSIS
Functions/Subroutines DOUBLE PRECISION function dla_gerpvgrw (N, NCOLS, A, LDA, AF, LDAF) DLA_GERPVGRW Function/Subroutine Documentation DOUBLE PRECISION function dla_gerpvgrw (integerN, integerNCOLS, double precision, dimension( lda, * )A, integerLDA, double precision, dimension( ldaf, * )AF, integerLDAF) DLA_GERPVGRW Purpose: DLA_GERPVGRW computes the reciprocal pivot growth factor norm(A)/norm(U). The "max absolute element" norm is used. If this is much less than 1, the stability of the LU factorization of the (equilibrated) matrix A could be poor. This also means that the solution X, estimated condition numbers, and error bounds could be unreliable. Parameters: N N is INTEGER The number of linear equations, i.e., the order of the matrix A. N >= 0. NCOLS NCOLS is INTEGER The number of columns of the matrix A. NCOLS >= 0. A A is DOUBLE PRECISION array, dimension (LDA,N) On entry, the N-by-N matrix A. LDA LDA is INTEGER The leading dimension of the array A. LDA >= max(1,N). AF AF is DOUBLE PRECISION array, dimension (LDAF,N) The factors L and U from the factorization A = P*L*U as computed by DGETRF. LDAF LDAF is INTEGER The leading dimension of the array AF. LDAF >= max(1,N). Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Definition at line 100 of file dla_gerpvgrw.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.2 Tue Sep 25 2012 dla_gerpvgrw.f(3)
All times are GMT -4. The time now is 05:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy