- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习使用 PowerShell 编写脚本,我发现这段代码可以帮助我完成一个项目 该示例来自 Is there a one-liner for using default values with Read-Host? .
$defaultValue = 'default'
$prompt = Read-Host "Press enter to accept the default [$($defaultValue)]"
$prompt = ($defaultValue,$prompt)[[bool]$prompt]
我想我明白 $prompt = ($defaultValue,$prompt)
正在创建一个二元素数组,并且 [bool]
部分正在强制 $prompt
数据类型为 bool 值,但我不明白第三行代码作为一个整体的作用。
最佳答案
这是一种常见的编程模式:
if (user entered a price)
{
price = user entered value
}
else
{
price = default value
}
因为这很常见,而且也很冗长,所以一些语言有一个特殊的三元运算符
来更简洁地编写所有代码,并将变量分配给“这个值或那个值”一动。例如在 C# 中你可以这样写:
price = (user entered a price) ? (user entered value) : (default value)
# var = IF [boolean test] ? THEN (x) ELSE (y)
如果测试为 true,则 ?
分配 (x)
,如果测试为 false,则分配 (y)
。
在Python中,它是这样写的:
price = (user entered value) if (user entered a price) else (default value)
在 PowerShell 中,它是这样写的:
# you can't have a ternary operator in PowerShell, because reasons.
是的。不允许使用漂亮的短代码模式。
但是你可以做的是滥用数组索引(@('x', 'y')[0] is 'x'
和 @('x', ' y')[1] 是 'y'
和 ) 并编写丑陋且令人困惑的代码高尔夫行:
$price = ($defaultValue,$userValue)[[bool]$UserEnteredPrice]
# var (x,y) is an array $array[ ] is array indexing
(0,1) are the array indexes of the two values
[bool]$UserEnteredPrice casts the 'test' part to a True/False value
[True/False] used as indexes into an array indexing makes no sense
so they implicitly cast to integers, and become 0/1
# so if the test is true, the $UserValue is assigned to $price, and if the test fails, the $DefaultValue is assigned to price.
它的行为类似于三元运算符,只是它令人困惑且丑陋,并且在某些情况下,如果您不小心评估两个数组表达式,无论选择哪一个,它都会让您陷入困境(与真正的?
运算符)。
编辑:我真正应该添加的是我更喜欢的 PowerShell 表单 - 您可以直接在 PowerShell 中分配 if
测试的结果并执行以下操作:
$price = if ($userValue) { $userValue } else { $DefaultValue }
# ->
$prompt = if ($prompt) { $prompt } else { $DefaultValue }
关于powershell - $prompt = ($defaultValue,$prompt)[[bool]$prompt] - 在 PowerShell 中模拟三元条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40892156/
这并不像看起来那么微不足道。这是 this question 的后续行动. 假设我有一个带有属性的 Windows 窗体用户控件: // using System.ComponentModel; [D
我对原生 javascript defaultValue 功能有点困惑。我已经了解必须设置 html 输入的值,并且该值是初始默认值。 当我在点击事件中有一个特定的输入(文本)元素时,我想将其恢复为其
我正在尝试设置多分支管道配置,其中“部署” bool 复选框默认为 true在非生产分支上,以及 false在生产构建上。 pipeline { parameters{ booleanPa
我不知道为什么这不起作用 Demo 我有以下 es6 代码 const {createFactory, createClass, DOM: { label, input, button }} = Re
这个问题已经有答案了: Default value for public properties (2 个回答) 已关闭 7 年前。 我正在使用C# windows窗体控件库程序来创建自己的控件,代码如
这个问题已经有答案了: Default value for public properties (2 个回答) 已关闭 7 年前。 我正在使用C# windows窗体控件库程序来创建自己的控件,代码如
普通 HTML 下拉菜单: Before redirecting to PayPal After payment is successful Never send em
在 JAX-RS 中,我可以定义一个查询参数来填充列表: @GET @Path("/foo") public String myService( @DefaultValue("def
我有以下汽车属性 [DefaultValue(true)] public bool RetrieveAllInfo { get; set; } 当我尝试在代码中使用它时,我发现默认的 false 是
谁能告诉我在变更集中指定defaultValue =“0”与defaultValueNumeric =“0”之间的区别吗?用于bigint列。 http://www.liquibase.org/man
我正在尝试使用 createContext 构建上下文来自 react API: import React, { useState, createContext } from 'react'; exp
我注意到,当我为下拉列表设置我的 defaultValue 时,尽管当我第一次将组件添加到页面时在下拉列表中正确选择了它,它不会将 defaultValue 写入相应的 JCR,直到我编辑组件并保存它
我有一个使用jax-rs的项目,我想以注释@DefaultValue的方式生成javadoc 我怎样才能用gradle实现呢? 最佳答案 我想你可以做类似的事情 javadoc { optio
我有一个 React 组件,它呈现 3 个选择元素。我想为所有 3 个设置默认值,但当我尝试这样做时,它不起作用。 我使用传入的名为 type 的属性来区分每一个。类型是每个选择元素的名称。我的 de
假设我具有以下域类: class DomainClass{ String field1 ... static mapping = { field1 defaultValu
在域中,我对状态属性有以下说明 static mapping={ status defaultValue="NNE" } static constraints={ status(null
我正在尝试将流反序列化为具有默认值属性的对象,而 protobuf-net 只是在流中缺少这些属性时才吸收这些属性。我是否需要在反序列化之前手动将所有属性设置为其默认值? 最佳答案 DefaultVa
当我为我的服务指定 @DefaultValue 时,在没有提供参数的情况下调用它时,为什么它只能是字符串,而不能是任何其他类型?下面的 @DefaultValue(0) 会抛出错误“无法从 int 转
我想知道是否有办法将 radio 重置为其最初选择的选项。我知道用于输入的 defaultValue 但有没有办法让它在页面加载时将 radio 重置为最初选择的值? 我不想简单地取消选择所有 rad
我使用 RingtonePreference 并希望使用默认通知声音作为默认首选项值。为此,我遵循了以下建议:RingtonePreference Default value 可悲的是,实际上我得到了
我是一名优秀的程序员,十分优秀!