Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

optimal expansions?



Script:

    grn=$'\e[32;1m'
    nrm=$'\e[0m'

    var=( "${(@f)$(apt-file search $1)}" )
    targ=
    var2=()
        
    for ((i=1; i<=$#var; i++ )); do
        if [[ "$targ" != "${${=var[i]}[1]}" ]]; then
            targ="${${=var[i]}[1]}"
            var2+="\n${grn}${${=var[i]}[1]}${nrm}" # Copy first word of line.
        fi
        var2+="${${=var[i]}[2,-1]}" # Copy the rest of the line no matter how many words.
    done
        
    print -l "$var2[@]"

---------------------------------------------------

That's my preferred way to look at 'apt-file search' output (Debian and derivatives only of course).  It works fine and I think I understand all the expansions and splitting.  One you get used to it the nested expansions aren't so scary, just read them from inside out, one step at a time and it's easy.  But is it optimal?  I've been known to go one step to the right by first going three steps to the left, then four steps to the right.  I'm guessing it's tight, but ...

BTW, I had a much simpler way of doing this based on a 'two words per line of output' assumption, but Debian, in their wisdom, have a very few installable files that have spaces in their names, so the above: "   [2,-1]   " way of doing things is needed.  I have to split on lines and then sub-split on words ... I think.  But I do have a talent for making things harder than they need be.




Messages sorted by: Reverse Date, Date, Thread, Author