-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested member access (dot notation) on a number-like property name fails #14036
Comments
Although you're probably aware of it I just want to mention for others who have this issue that this can be worked around for now by quoting the property:
This also works for other property-names that make PowerShell misbehave, such as properties starting with a '#' etc. |
Thanks, @jantari - I've added the workaround to the OP. Note that |
There is actually a lexical ambiguity here. Consider ([pscustomobject] @{ 1 = 'foo ' }).(1).Trim() |
Sort of? Property access tends to convert basically everything to a string in most cases, because most properties can only have strings for the property name. PSObject / PSCustomObject creates objects with string property names, not integer ones. So, yes, that works... incidentally only. There's no reason not to assume a string value is being used for the property name IMO. The only exception is dictionaries/hashtables having non-string key types. Regular objects have strings for property names, I don't think there are really any exceptions to that. It's already true in some cases that you must use |
Agreed, @vexx32:
PS> @{ 1.5 = 'foo ' }.1.5.Trim()
foo If we were to maintain strict backward compatibility, we can't take this feature away. However, I've never seen a Given the presumed rarity of
If preserving backward compatibility is paramount, an alternative that at least ameliorates the problem - if technically feasible - would be to fall back to considering something like |
This comment has been minimized.
This comment has been minimized.
Longer-term, if we ever get to break backward compatibility more substantially, my recommendation would be, for conceptual simplicity and for avoiding edge cases:
(To soften the blow, the automatic |
Not sure if it's directly related but in below example nested properties also doesn't work unless put in quotes $l=@{I=1;V=5;X=10;L=50;C=100;D=500;M=1000}
$a='IVXLLD'
$l[$a[3]]
# Result:
$l["$($a[3])"]
# Result: 50 |
@catthehacker, no, this is a different problem: Your keys are By contrast, |
@mklement0 Thanks a lot, that's awfully helpful. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
2 similar comments
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This still fails in the latest preview so it's still relevant. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
Note: This is a relatively minor issue with an easy workaround, but it would be good to understand if there are other ramifications and a fix would certainly be nice.
Steps to reproduce
Expected behavior
Both tests should succeed.
Actual behavior
The 2nd test fails, because the
.1
is apparently no longer recognized as a property access in combination with the.Trim()
call:Note:
If the property name cannot be parsed as a number literal, the problem doesn't surface (e.g.,
.a1
).A related problem is trying to use a property name that starts with a digit; e.g.,
([pscustomobject] @{ '1a' = 'foo ' }).1a
breaks, unless.'1a'
is used instead.Workarounds:
([pscustomobject] @{ 1 = 'foo ' }).'1'.Trim()
(...)
:(([pscustomobject] @{ 1 = 'foo ' }).1).Trim()
$Matches
variable:$null = 'foo bar' -match '^(foo\s)'; ($Matches.1).Trim()
(...)
also works in both scenarios:([pscustomobject] @{ 1 = 'foo ' }).(1).Trim()
$null = 'foo bar' -match '^(foo\s)'; $Matches.(1).Trim()
Environment data
The text was updated successfully, but these errors were encountered: