Archiv verlassen und diese Seite im Standarddesign anzeigen : mehrdimensionale arrays in der shell?
Hallo,
ich versuche mich gerade an mehrdimensionalen arrays in der shell (bisher mit bash, waere aber auch mit csh glucklich - genaugenommen mit jeder shell, die das kann). Da ich weder hier im Forum, noch in anderen Foren, noch mit google was passendes gefunden habe... geht das etwa nicht?
Erfolglose Versuche:
$tstarr[0,1]
$tstarr([0][1])
$tstarr([0],[1])
${tstarr_$i_$j}
tstarr=arr_$i_$j
${tstarr}=xxx
Nein, ich hatte nicht bei allen daran geglaubt, dass es funktionieren wuerde ;)
Mit awk (soweit wuerde ich noch gehen, perl und so eher nicht mehr) hat sowas wohl jemand schon gemacht, ich verstehs aber nicht, weiss auch nicht, obs mir helfen wuerde:
http://www.fz-rossendorf.de/FVTK/SERVER/awk.html#mehrarray (http://http://www.fz-rossendorf.de/FVTK/SERVER/awk.html#mehrarray)
Ich hoffe auf Eure Hilfe
TIA
cu
kamome
peschmae
16-02-2004, 06:15
Da steht was dazu: http://www.tldp.org/LDP/abs/html/arrays.html
Bash supports only one-dimensional arrays, however a little trickery permits simulating multi-dimensional ones.
Example 26-16. Simulating a two-dimensional array, then tilting it
#!/bin/bash
# twodim.sh: Simulating a two-dimensional array.
# A two-dimensional array stores rows sequentially.
Rows=5
Columns=5
declare -a alpha # char alpha [Rows] [Columns];
# Unnecessary declaration. Why?
load_alpha ()
{
local rc=0
local index
for i in A B C D E F G H I J K L M N O P Q R S T U V W X Y
do # Use different symbols if you like.
local row=`expr $rc / $Columns`
local column=`expr $rc % $Rows`
let "index = $row * $Rows + $column"
alpha[$index]=$i
# alpha[$row][$column]
let "rc += 1"
done
# Simpler would be
#+ declare -a alpha=( A B C D E F G H I J K L M N O P Q R S T U V W X Y )
#+ but this somehow lacks the "flavor" of a two-dimensional array.
}
print_alpha ()
{
local row=0
local index
echo
while [ "$row" -lt "$Rows" ] # Print out in "row major" order -
do # columns vary
# while row (outer loop) remains the same.
local column=0
while [ "$column" -lt "$Columns" ]
do
let "index = $row * $Rows + $column"
echo -n "${alpha[index]} " # alpha[$row][$column]
let "column += 1"
done
let "row += 1"
echo
done
# The simpler equivalent is
# echo ${alpha } | xargs -n $Columns
echo
}
filter () # Filter out negative array indices.
{
echo -n " " # Provides the tilt.
# Explain why.
if [[ "$1" -ge 0 && "$1" -lt "$Rows" && "$2" -ge 0 && "$2" -lt "$Columns" ]]
then
let "index = $1 * $Rows + $2"
# Now, print it rotated.
echo -n " ${alpha[index]}"
# alpha[$row][$column]
fi
}
rotate () # Rotate the array 45 degrees
{ #+ ("balance" it on its lower lefthand corner).
local row
local column
for (( row = Rows; row > -Rows; row-- ))
do # Step through the array backwards.
for (( column = 0; column < Columns; column++ ))
do
if [ "$row" -ge 0 ]
then
let "t1 = $column - $row"
let "t2 = $column"
else
let "t1 = $column"
let "t2 = $column + $row"
fi
filter $t1 $t2 # Filter out negative array indices.
done
echo; echo
done
# Array rotation inspired by examples (pp. 143-146) in
#+ "Advanced C Programming on the IBM PC", by Herbert Mayer
#+ (see bibliography).
}
#--------------- Now, let the show begin. ------------#
load_alpha # Load the array.
print_alpha # Print it out.
rotate # Rotate it 45 degrees counterclockwise.
#-----------------------------------------------------#
# This is a rather contrived, not to mention kludgy simulation.
#
# Exercises:
# ---------
# 1) Rewrite the array loading and printing functions
# + in a more intuitive and elegant fashion.
#
# 2) Figure out how the array rotation functions work.
# Hint: think about the implications of backwards-indexing an array.
exit 0
A two-dimensional array is essentially equivalent to a one-dimensional one, but with additional addressing modes for referencing and manipulating the individual elements by "row" and "column" position.
MfG Peschmä
Wow, danke peschmae! Das wird mir helfen.
Fuer das aktuelle Problem habe ich mich Inzwischen allerdings schon auf ein eindimensionales array beschraenkt - auch dabei gibt es nun aber ein Problem. Siehe bitte auch naechsten thread "seltsames return von Funktion in bash".
cu
kamome
Powered by vBulletin® Version 4.2.5 Copyright ©2024 Adduco Digital e.K. und vBulletin Solutions, Inc. Alle Rechte vorbehalten.