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

# Re: Looking for devious one or two liner solutions to a problem

• X-seq: zsh-users 11983
• From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
• To: Kenneth Lareau <elessar@xxxxxxxxxxx>, zsh-users@xxxxxxxxxx
• Subject: Re: Looking for devious one or two liner solutions to a problem
• Date: Wed, 10 Oct 2007 20:35:09 -0700
• Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
• References: <20071010163733.7875cee3.elessar@xxxxxxxxxxx>

```On Oct 10,  4:37pm, Kenneth Lareau wrote:
} Subject: Looking for devious one or two liner solutions to a problem
}
} 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;
}    }

At the risk of turning this into a golf match ...

sub bump_version {
my @a = split(/\./, \$_[0]);
splice(@a, \$_[1], 4, \$a[\$_[1]]+1, 0, 0, 0);
join('.', @a[0..3]);
}

In zsh you can do the same:

bump_version() {
local -a a; a=(\${(s:.:)1})
a[\$2+1,4]=(\$[\$a[\$2+1]+1] 0 0 0)
print \${(j:.:)\${a[1,4]}}
}

If you dispense with "local -a a;" that's 72 characters for the body,
and sticking f(){...} around it adds 5 for 77.

f(){a=(\${(s:.:)1});a[\$2+1,4]=(\$[\$a[\$2+1]+1] 0 0 0);print \${(j:.:)\${a[1,4]}}}

If you assume "setopt ksharrays" you can cut it to 75:

f(){a=(\${(s:.:)1});a[\$2,3]=(\$[\${a[\$2]}+1] 0 0 0);print \${(j:.:)\${a[0,3]}}}

And if you return the new version in \$a (since you've already made it
non-local) and therefore don't bother printing it, 71 characters:

f(){a=(\${(s:.:)1});a[\$2,3]=(\$[\${a[\$2]}+1] 0 0 0);a=\${(j:.:)\${a[0,3]}}}

```

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