Вопрос

This seems pretty petty but I am writing a PowerShell script which will write an email to a user, and it will refer to a number of "days" remaining taken from a variable ($DaysLeft).

To get this gramatically correct (in terms of "day"/"days"), I could (in Oracle SQL) use the DECODE function, thus:

DaysLeft ||' day'||DECODE(DaysLeft, 1, '', 's')||' Remaining'

Obviously you could do something similar with CASE, and that would be just as satisfactory.

I am wondering if PowerShell has a similar self-contained block I can use for this purpose. I could obviously define a variable with an IF-ELSE block, but I am hoping there is a more efficient way!

I saw some examples with the SWITCH command, but this doesn't seem to be what I'm after... or is it?

Thanks!

Это было полезно?

Решение

You could use a switch like this:

$numdays = 1
$phrase = switch ($numdays){1 {'is 1 day'};default {"are $_ days"}}
"There $phrase remaining."
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top