Differences

This shows you the differences between two versions of the page.

Link to this comparison view

array [2012/11/04 20:58]
array [2022/04/16 12:22] (current)
Line 1: Line 1:
 +@related: [[arpalhands]]  [[bash]]
 +
 +
 +<code type=bash>
 +#! /bin/bash
 +
 +function check_input
 +{
 +    nb=$(wc -c ${file_set2[0]} ${file_set2[*]} \
 + | awk '{print $1}' | sed '$d' | sort -u | wc -l)
 +    [ ${nb} != 1 ] && echo "Error: All input files must have same size !"
 +    [ ${nb} = 1 ]
 +}
 +
 +if [ ${#} -lt 3 ]
 +then
 +    cat << EOF
 +Usage: ${0##*/} element_size infile1 [ infile2 ... infileN ] outfile
 +element_size : size in bytes of element to read in each input files
 +EOF
 +    exit
 +fi
 +
 +element_size="${1}"
 +shift
 +file_set=("${@}")
 +idx=0
 +while [ 0${idx} -lt 0$((${#file_set[*]}-1)) ] ; do
 +    file_set2[idx]="$(mktemp)"
 +    xxd -c ${element_size} -p "${file_set[idx]}" > "${file_set2[idx]}"
 +    ((++idx))
 +done
 +check_input && paste -d '\n' ${file_set2[*]} \
 + | xxd -p -r > "${file_set[$((${#file_set[*]}-1))]}"
 +rm -f "${file_set2[*]}"
 +</code>
 +
 +
 +
 +
 +====== Contexte ======
 +
 +J'ai N tableaux. 
 +Chacun de ces N tableaux n'ayant pas forcément la même taille.
 +Le but est de fournir une plage d'index pour chacun des N tableaux et que le programme calcule la combinatoire.
 +
 +Exemple:\\
 +array1=(a b)\\
 +array2=(c d e)\\
 +array3=(f g h i)
 +
 +Donc 3 plages d'index :\\
 +{0,1}\\
 +{0,1,2}\\
 +{0,1,2,3}
 +
 +Imaginons que je sois intéressé par :\\
 +le premier index pour le premier tableau\\
 +les index 1 et 2 pour le 2e\\
 +les index 0, 1 et 2 pour le dernier
 +
 +en bash : compute_indexes 0 {1,2} {0..2}\\
 +la sortie : 0 1 0 0 1 1 0 1 2 0 2 0 0 2 1 0 2 2 \\
 +si je l'affiche par triplets :\\
 +0 1 0\\
 +0 1 1\\
 +0 1 2\\
 +0 2 0\\
 +0 2 1\\
 +0 2 2 
 +
 +====== Code ======
 +
 +<code bash>
 +expand_user_input()
 +{
 +  local idxs=()
 +  local idx
 +  for ((idx=0; idx<${#REPLY[@]}; ++idx))
 +  do
 +    REPLY[idx]="$(eval echo ${REPLY[idx]})"
 +    idxs+=(0)
 +  done
 +  iterator()
 +  {
 +    local idx=-1
 +    while ((-${#REPLY[@]} <= ${idx}))
 +    do
 +      local tmp=(${REPLY[idx]})
 +      idxs[idx]=$(((idxs[idx]+1)%${#tmp[@]}))
 +      [ ${idxs[idx--]} -ne 0 ] && return
 +    done
 +    false
 +  }
 +  local result
 +  while true
 +  do
 +    for ((idx=0; idx<${#REPLY[@]}; ++idx))
 +    do
 +      local tmp=(${REPLY[idx]})
 +      result+="${tmp[${idxs[idx]}]} "
 +    done
 +    iterator || break
 +  done
 +  echo "${result}"
 +}
 +</code>
 +
 +
 +====== Comment s'en sert-on ? ======
 +
 +
 +Cas N=4\\
 +REPLY=(\{0..10\} 2 3 4)\\
 +expand_user_input
 +
 +0 2 3 4 1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4 5 2 3 4 6 2 3 4 7 2 3 4 8 2 3 4 9 2 3 4 10 2 3 4
 +
 +Cas N=4\\
 +REPLY=(\{0,10\} 2 3 4)\\
 +expand_user_input
 +
 +0 2 3 4 10 2 3 4
 +
 +Cas N=4\\
 +REPLY=(\{0,2\} 2 \{1,3,5\} 4)\\
 +expand_user_input
 +
 +0 2 1 4 0 2 3 4 0 2 5 4 2 2 1 4 2 2 3 4 2 2 5 4
 +
 +Cas N=5\\
 +REPLY=(\{0,2\}  \{1,3,5\}  \{4,6\} \{7,9,11\} \{8,10,12,14\})\\
 +expand_user_input
 +
 +0 1 4 7 8 0 1 4 7 10 0 1 4 7 12 0 1 4 7 14 0 1 4 9 8 0 1 4 9 10 0 1 4 9 12 0 1 4 9 14 0 1 4 11 8 0 1 4 11 10 0 1 4 11 12 0 1 4 11 14 0 1 6 7 8 0 1 6 7 10 0 1 6 7 12 0 1 6 7 14 0 1 6 9 8 0 1 6 9 10 0 1 6 9 12 0 1 6 9 14 0 1 6 11 8 0 1 6 11 10 0 1 6 11 12 0 1 6 11 14 0 3 4 7 8 0 3 4 7 10 0 3 4 7 12 0 3 4 7 14 0 3 4 9 8 0 3 4 9 10 0 3 4 9 12 0 3 4 9 14 0 3 4 11 8 0 3 4 11 10 0 3 4 11 12 0 3 4 11 14 0 3 6 7 8 0 3 6 7 10 0 3 6 7 12 0 3 6 7 14 0 3 6 9 8 0 3 6 9 10 0 3 6 9 12 0 3 6 9 14 0 3 6 11 8 0 3 6 11 10 0 3 6 11 12 0 3 6 11 14 0 5 4 7 8 0 5 4 7 10 0 5 4 7 12 0 5 4 7 14 0 5 4 9 8 0 5 4 9 10 0 5 4 9 12 0 5 4 9 14 0 5 4 11 8 0 5 4 11 10 0 5 4 11 12 0 5 4 11 14 0 5 6 7 8 0 5 6 7 10 0 5 6 7 12 0 5 6 7 14 0 5 6 9 8 0 5 6 9 10 0 5 6 9 12 0 5 6 9 14 0 5 6 11 8 0 5 6 11 10 0 5 6 11 12 0 5 6 11 14 2 1 4 7 8 2 1 4 7 10 2 1 4 7 12 2 1 4 7 14 2 1 4 9 8 2 1 4 9 10 2 1 4 9 12 2 1 4 9 14 2 1 4 11 8 2 1 4 11 10 2 1 4 11 12 2 1 4 11 14 2 1 6 7 8 2 1 6 7 10 2 1 6 7 12 2 1 6 7 14 2 1 6 9 8 2 1 6 9 10 2 1 6 9 12 2 1 6 9 14 2 1 6 11 8 2 1 6 11 10 2 1 6 11 12 2 1 6 11 14 2 3 4 7 8 2 3 4 7 10 2 3 4 7 12 2 3 4 7 14 2 3 4 9 8 2 3 4 9 10 2 3 4 9 12 2 3 4 9 14 2 3 4 11 8 2 3 4 11 10 2 3 4 11 12 2 3 4 11 14 2 3 6 7 8 2 3 6 7 10 2 3 6 7 12 2 3 6 7 14 2 3 6 9 8 2 3 6 9 10 2 3 6 9 12 2 3 6 9 14 2 3 6 11 8 2 3 6 11 10 2 3 6 11 12 2 3 6 11 14 2 5 4 7 8 2 5 4 7 10 2 5 4 7 12 2 5 4 7 14 2 5 4 9 8 2 5 4 9 10 2 5 4 9 12 2 5 4 9 14 2 5 4 11 8 2 5 4 11 10 2 5 4 11 12 2 5 4 11 14 2 5 6 7 8 2 5 6 7 10 2 5 6 7 12 2 5 6 7 14 2 5 6 9 8 2 5 6 9 10 2 5 6 9 12 2 5 6 9 14 2 5 6 11 8 2 5 6 11 10 2 5 6 11 12 2 5 6 11 14
 +
  
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki