question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behaviour while populating values in an ordered hash table

See original GitHub issue

Prerequisites

Steps to reproduce

Create an ordered hash table and populate some of its properties with values previously defined in the hash table. Sometimes the values get populated, sometimes not.

    $Api = [ordered]@{
        apiToken = ""
        validUntil = $(Get-Date)
        baseUrl = "https://someUrl.com"
        apiEndPoint = "api/config/v1"
        vaultEndpoint = "$($Api.baseUrl)/$($Api.apiEndPoint)/vault/account"
        tokenEndpoint = "$($Api.baseUrl)/oauth2/token"
        clientId = ""
        clientSecret = ""
    }

Expected behavior

PS> Write-Host $Api.tokenEndpoint
https://someUrl.com/oauth2/token

Actual behavior

PS> Write-Host $Api.tokenEndpoint
oauth2/token

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.6
PSEdition                      Core
GitCommitId                    7.2.6
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jhoneillcommented, Aug 26, 2022

IIRC If you run the code with no pre-existing hash table , $hashtable.key will be null. If you run it to recreate an existing hash table, it will use the pre-existing value, not the value you are putting into the new version.

2reactions
mklement0commented, Aug 27, 2022

A simpler and more robust workaround, as also shown in the linked issue, is to use aux. variables:

$dict =
  [ordered] @{
    url = ($url = 'example.com')
    endpoint = "$url/api/config"
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow HashTable to reference itself · Issue #13782
Allow HashTable to reference itself #13782 ... Unexpected behaviour while populating values in an ordered hash table #17971.
Read more >
Unexpected hash's behavior - ruby
First, some comments: Your code is not indented in a way that most others do it, which makes it hard for others to...
Read more >
Everything you wanted to know about hashtables
Hashtables are really important in PowerShell so it's good to have a solid understanding of them.
Read more >
An unexpected PowerShell HashTable feature
The cause of this unexpected behaviour is the second hashtable entry we inserted: Keys. This hashtable entry hides the PowerShell hashtable built-in Keys ......
Read more >
Build a Hash Table in Python With TDD
In this step-by-step tutorial, you'll implement the classic hash table data structure using Python. Along the way, you'll learn how to cope with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found