Wednesday, March 28, 2007

Confirmado: uno vé @hotmail y piensa....

... lo mismo que esta empresa de recursos humanos al momento de contratar especialistas de Internet y afines:

Link: Catorze.blog - Blog Archive - El criterio Hotmail

Así que ya sabés: no pongas ese @hotmail en tu Currículum :-P

Sunday, March 04, 2007

[en] powerful arrays in bash

Doing some experiments with advanced bash array features. I created a bash implementation of insertion sort algorithm.

UPDATE: This example is already part of the great Advanced Shell Scripting Guide :)


Enjoy :-D




#!/bin/bash
# insertion-sort.bash.sh: Insertion sort implementation in bash
# Heavy use of bash array features: slicing, merging, etc
# URL: http://www.lugmen.org.ar/~jjo/jjotip/insertion-sort.bash.d/insertion-sort.bash.sh
#
# Author: JuanJo Ciarlante jjo \O/ irrigacion gov ar
# License: GPLv2
#
# Test with: ./insertion-sort.bash.sh -t
#

: ${DEBUG:=1} # debug, override with: DEBUG=1 ./scriptname ..

# Global array: "list"
typeset -a list
# Load whitespace separated numbers from just stdin 1st line
if [ "$1" = "-t" ];then
read -a list < <(od -An -w32 -t u2 /dev/urandom )
else
read -a list
fi
numelem=${#list[*]}

# Shows the list, marking the element whose index es $1 by surrounding it with
# the two chars passed as $2; whole line prefixed with $3
showlist() { echo "$3"${list[@]:0:$1} ${2:0:1}${list[$1]}${2:1:1} ${list[@]:$1+1}; }

# loop "pivot" from 2nd elem, to end of list
for((i=1;i<numelem;i++))do
((DEBUG))&&showlist i "[]" " "
# From current "pivot", back to 1st elem
for((j=i;j;j--))do
# search for the 1st elem less than current "pivot" ...
[[ "${list[j-1]}" -le "${list[i]}" ]] && break
done
((i==j)) && continue ## no insertion was needed for this element
# ... move list[i] (pivot) to the left of list[j]:
list=(${list[@]:0:j} ${list[i]} ${list[j]} ${list[@]:j+1:i-(j+1)} ${list[@]:i+1})
# {0,j-1} {i} {j} {j+1,i-1} {i+1,last}
((DEBUG))&amp;amp;amp;&showlist j "<>" "*"
done
echo $'Result:\n'${list[@]}