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

Looking for devious one or two liner solutions to a problem



At my workplace, we have a given problem we give potential engineers to see how their
programming and thought process skills are; it's a relatively simple problem:

   Given a version string of the form a.b.c.d (e.g. 5.3.20.4) and an index of 0-3
   (representing the position of a given number in the version string, zero-based),
   write a function that will take the two as parameters and return a new version
   string which has the number in the index position given incremented by one and
   all successive numbers set to 0.  Examples:

        5.3.20.4, 0  ->  6.0.0.0
        8.14.0.5  1  ->  8.15.0.0
        3.9.42.6  3  ->  3.9.42.7

In Perl, I was able to come up with the very simple function below:

   sub bump_version {
       my ($vers, $ind) = @_;

       @vers = split /\./, $vers;
       $vers[$ind] += 1;
       for ($i = $ind + 1; $i < 4; $i++) {
           $vers[$i] = 0;
       }

       return join '.', @vers;
   }

I've come up with something very similar in zsh, but I'm extremely curious;
is there anyone out there who knows zsh well enough to create a highly compressed
version of this function that would fit on a single 80 character line (or perhaps
two such lines)?  While I've been using zsh for many years, I haven't delved deep
enough into it to be able to figure anything out, but if someone else could, I'd
be very appreciative to see it!

Ken

-- 
Ken Lareau
elessar@xxxxxxxxxxx



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