|
||||||
| Forums FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Hi,
I am trying to implement a simple bubble sort program in bash. However, I seem to be facing some problems while operating on arrays. So far, I have been using the Advanced Bash Shell Guide on tldp.org as my reference, but even the syntax given there does not seem to work. I would be grateful if someone here in this forum could try to look into this matter: 19 if [ ${array["$comp_pos"]} -lt ${array["$j"]} then 20 temp = ${array[${comp_pos}]} 21 {array[${comp_pos}]} = ${array[${j}]} 22 {array[${j}]}=$temp 23 fi Thanks for going through this post. I am getting the following error message: 8.sh: line 20: temp: command not found 8.sh: line 21: {array[2]}: command not found 8.sh: line 22: {array[1]}=: command not found 8.sh: line 20: temp: command not found 8.sh: line 21: {array[3]}: command not found 8.sh: line 22: {array[2]}=: command not found |
|
||||
|
I am afraid most of the error messages have nothing to do with the usage of arrays. The are just simple errors you made.
Code:
temp = ${array[${comp_pos}]}
1. = 2. what ${array[${comp_pos}]} becomes after bash did all substitutions. What you probably want is: Code:
temp=${array[${comp_pos}]}
Code:
{array[${comp_pos}]} = ${array[${j}]}
Code:
{array[2]} = someresult
Orr as ana alternative (i do not know what you want to achieve exactly) you wanted the { } removed. In short you should better read what you have written, and then ask yourself: how many fases will this go through and what happens in every of those phases. The man pages of bash come to help here. This involves things like: alias substitution, parameter substitution. ..., program exececution. I will leave the rest to you. You can of course come back with further questions, but I advise you first to write some simple scripts to train yourself and find out about the wonderfull (im)possibilities of bash
__________________
Henk van Velden |
|
||||
|
In the additional info Henk provided above, it seems you don't really seem to understand one-dimentional arrays in Bash. I'll give a very simple example how to create and call them...
Code:
for i in {1..3}; do
myvar[i]="value$i"
done
echo "${myvar[*]}"
__________________
My site: http://microchip.bplaced.net My repo: http://download.opensuse.org/repositories/home:/microchip8 SUSE Unbound Forum: http://suseunbound.lefora.com Do coders dream of sheep() ? |
|
|||
|
Yes, i have got it. As I said before, I was trying to perform a bubble sort, and the fragment of the code tries to swap values, in case the condition is satisfied. From what I gathered from your replies, and the links where I was referred, I found that this is what the code should be:
PHP Code:
|
|
||||
|
No problemo, ask anytime.... and what? Henk and me get no reputation points?
__________________
My site: http://microchip.bplaced.net My repo: http://download.opensuse.org/repositories/home:/microchip8 SUSE Unbound Forum: http://suseunbound.lefora.com Do coders dream of sheep() ? |
|
||||
|
Was away for one and a half day. Look if your programming style improved immensly (with all the nice colours). But still no reps
__________________
Henk van Velden |
|
||||
|
What can I say, Henk? Unthankful cretins... why do we even bother?
__________________
My site: http://microchip.bplaced.net My repo: http://download.opensuse.org/repositories/home:/microchip8 SUSE Unbound Forum: http://suseunbound.lefora.com Do coders dream of sheep() ? |
|
||||
|
I agree, but maybe the place where to click for it should be a bit more eye catching. I did not realy understand what it was for until, by incident, I read something about in a post.
In any case, he said: "it works". And he said: "thank you". Much more then many do
__________________
Henk van Velden |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|