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

Re: can I control brace expansion's "sort"/"display" order?



Mikael,

Eric De Mund <ead@xxxxxxxxx>:
> Can I control brace expansion's "sort" or "display" order?
>
> For instance this zsh echo results in this output:
>
>    % echo {white,black}{bird,fish}{21..24} | tr ' ' '\n'
>    # current output order
>    whitebird21
>    whitebird22
>    whitebird23
>    whitebird24
>    whitefish21
>    whitefish22
>    whitefish23
>    whitefish24
>    blackbird21
>    blackbird22
>    blackbird23
>    blackbird24
>    blackfish21
>    blackfish22
>    blackfish23
>    blackfish24
>    %
>
> But what if instead of this order I wanted the last brace to cycle
> most slowly, followed by the first, followed by the second, like so:
>
>    % echo {white,black}{bird,fish}{21..24} | tr ' ' '\n'
>    # desired output order
>    whitebird21
>    whitefish21
>    blackbird21
>    blackfish21
>    whitebird22
>    whitefish22    # oops: I had these two swapped in my original email
>    blackbird22    # oops: I had these two swapped in my original email
>    blackfish22
>    whitebird23
>    whitefish23
>    blackbird23
>    blackfish23
>    whitebird24
>    whitefish24
>    blackbird24
>    blackfish24
>    %
>
> Is there any way to accomplish this?
>
> My problem is that I'm trying to get files added to a zip archive in
> a particular order, and this order is neither brace expansion sort/
> display order nor lexicographical order.

Mikael Magnusson <mikachu@xxxxxxxxx>:
] You can probably do it with some for-looping, like
] setopt shortloops
] % for a in white black; for b in fish bird; for c in {21..24}; echo $a$b$c
] whitefish21
] whitefish22
] whitefish23
] whitefish24
] whitebird21
] whitebird22
] whitebird23
] whitebird24
] blackfish21
] blackfish22
] blackfish23
] blackfish24
] blackbird21
] blackbird22
] blackbird23
] blackbird24
]
] Now you can just swap the order of the loops
] % for b in fish bird; for a in white black; for c in {21..24}; echo $a$b$c
] whitefish21
] whitefish22
] whitefish23
] whitefish24
] blackfish21
] blackfish22
] blackfish23
] blackfish24
] whitebird21
] whitebird22
] whitebird23
] whitebird24
] blackbird21
] blackbird22
] blackbird23
] blackbird24

Thank you very much! This is great. This works for me. I'd use the
following solution, having:

o   the first variable represent the slowest cycling field (in my case
    the third field),
o   the second variable represent the next slowest cycling field (in my
    case the first field), and
o   the third variable represent the next slowest cycling field (in my
    case the second field).

Like so:

    % for a in {21..24}; for b in {white,black}; for c in {bird,fish}; \
    echo $b$c$a
    whitebird21
    whitefish21
    blackbird21
    blackfish21
    whitebird22
    whitefish22
    blackbird22
    blackfish22
    whitebird23
    whitefish23
    blackbird23
    blackfish23
    whitebird24
    whitefish24
    blackbird24
    blackfish24
    % 

Regards,
Eric
--
Eric De Mund   | Ixian Systems           | Jab: eadixian@xxxxxxxxxx/main
ead@xxxxxxxxx  | 650 Castro St, #120-210 | Y!M: ead0002
ixian.com/ead/ | Mountain View, CA 94041 | ICQ: 811788



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