#!/bin/bash
# getMaxLen - bash function to retrive longest len from an array
# (C) 2025 Felix Hauri - felix@f-hauri.ch
#
getMaxLen() { 
    if [[ $1 == -v ]]; then local __set_var=$2; shift 2
    else local __set_var; fi
    local __tmp_string __tmp_array;
    printf -v __tmp_string ' %*s' ${#};
    __tmp_array=(${__tmp_string// / .});
    unset '__tmp_array[0]';
    __tmp_array=(${!__tmp_array[@]});
    local __tmp_array="(${__tmp_array[*]/*/[\${#&\}]= })";
    __tmp_array=(${!__tmp_array[@]});
    if [[ $__set_var ]]; then printf -v $__set_var %d ${__tmp_array[-1]}
    else echo ${__tmp_array[-1]}
    fi
}
