Immediate lookup of value in a hash table defined as a literal - powershell

What is the shortest and nicest way to have a hash table literal and instantly look up a value in it?
e.g. I was expecting something like
$city = #{"30328" = "Atlanta"; "60608" = "Chicago"} [$zipCode]
but that ends with:
Unexpected token '[$zipCode]' in expression or statement.

Just remove the space:
$city = #{"30328" = "Atlanta"; "60608" = "Chicago"}[$zipCode]
or
$city = #{"30328" = "Atlanta"; "60608" = "Chicago"}.$zipCode

Related

Crystal Lang convert string to hash and gsub

I have a string that looks like this
start
"contrib_11_FICO_Score = 0.7474262402579245 contrib_27_OtherPayments = -0.11397237107501418 contrib_20_LTV = 0.4209136780360599 contrib_0_AcctsOnUs = -0.195553721371136 contrib_19_InstallmentPayments = 0.08134999563389797 contrib_1_Amount = 0.07752028501333455 contrib_10_EstDebtToIncome = 0.19793275869285348 contrib_45_CVTE_ProductShortName.0 = 0.08545325753810393 contrib_21_LoanTerm = -0.20186524569828262 contrib_43_SatisfactoryLast2Yrs = 0.0710695727171011 contrib_50_WoE_ProductShortName.0 = -0.022073850549082376 contrib_31_PLCollectionTotal = 0.12617956675796796 contrib_40_RevBalToHighCredit = -0.5009070556076041 contrib_42_RevolvingPayments = 0.14787007118011708 contrib_39_RepoTotal = 0.002261663457841503 contrib_30_PLCollectionLast2yrs = 0.047241412208721086 contrib_15_InquiriesOnUs = -0.08118475273646548 contrib_17_InquiriesTotal = -0.23934745152247694 contrib_16_InquiriesToday = 0.06067597032236759 contrib_23_OilTE = -0.009115867521724635 contrib_14_InquiriesLast6mos = -0.01669536361883669 contrib_8_DTI = 0.40070958319864824 contrib_44_SatisfactoryTotal = -0.29471031930209096 contrib_22_Mortgage = -0.13973120155639737 contrib_3_AvgRevolvingBalance = -0.1339820646758586 contrib_41_RevolvingCount = 0.008316986809544236 contrib_32_Plus30Last2Yrs = 0.0315266336181764 contrib_29_OtherPublicTotal = 0.0025079738312499887 contrib_12_Inquiries30days = 0.08842218013671509 contrib_36_Plus90Last2Yrs = 0.03415210455083352 contrib_6_BankruptcyLast2yrs = 0.0037646391378712307 contrib_34_Plus60Last2Yrs = 0.014917255339436795 contrib_28_OtherPublicLast2yrs = 9.508765288605479E-4 contrib_bias = 0.7414680413223248"
and I'm trying to coerce it to a hash, like this.
end
{"contrib_11_FICO_Score"=>0.7474262402579245, "contrib_27_OtherPayments"=>-0.11397237107501418, "contrib_20_LTV"=>0.4209136780360599, "contrib_0_AcctsOnUs"=>-0.195553721371136, "contrib_19_InstallmentPayments"=>0.08134999563389797, "contrib_1_Amount"=>0.07752028501333455, "contrib_10_EstDebtToIncome"=>0.19793275869285348, "contrib_45_CVTE_ProductShortName"=>0.08545325753810393, "contrib_21_LoanTerm"=>-0.20186524569828262, "contrib_43_SatisfactoryLast2Yrs"=>0.0710695727171011, "contrib_50_WoE_ProductShortName"=>-0.022073850549082376, "contrib_31_PLCollectionTotal"=>0.12617956675796796, "contrib_40_RevBalToHighCredit"=>-0.5009070556076041, "contrib_42_RevolvingPayments"=>0.14787007118011708, "contrib_39_RepoTotal"=>0.002261663457841503, "contrib_30_PLCollectionLast2yrs"=>0.047241412208721086, "contrib_15_InquiriesOnUs"=>-0.08118475273646548, "contrib_17_InquiriesTotal"=>-0.23934745152247694, "contrib_16_InquiriesToday"=>0.06067597032236759, "contrib_23_OilTE"=>-0.009115867521724635, "contrib_14_InquiriesLast6mos"=>-0.01669536361883669, "contrib_8_DTI"=>0.40070958319864824, "contrib_44_SatisfactoryTotal"=>-0.29471031930209096, "contrib_22_Mortgage"=>-0.13973120155639737, "contrib_3_AvgRevolvingBalance"=>-0.1339820646758586, "contrib_41_RevolvingCount"=>0.008316986809544236, "contrib_32_Plus30Last2Yrs"=>0.0315266336181764, "contrib_29_OtherPublicTotal"=>0.0025079738312499887, "contrib_12_Inquiries30days"=>0.08842218013671509, "contrib_36_Plus90Last2Yrs"=>0.03415210455083352, "contrib_6_BankruptcyLast2yrs"=>0.0037646391378712307, "contrib_34_Plus60Last2Yrs"=>0.014917255339436795, "contrib_28_OtherPublicLast2yrs"=>9.508765288605479E-4, "contrib_bias"=>0.7414680413223248}
I can use gsub(" = ","=>")
.gsub(" contrib",","contrib").gsub("\","")
but I end up with
\"contrib
The backslashes prevent me from converting this to a hash. How can I strip the backslashes out? I would have thought that the escape character would work, but apparently not.
I also tried splitting the string on the space, but this gave me an array of strings and not a hash.
Thanks for any suggestions!

How to add data row to object

Below is the result of an API call via Invoke-Restmethod
And output of $test.result.organizationContext is as follows
How can I add an line item to this "organizationContext" object with values for the different attributes like " name", "id" ?
If we assume that you already have the values you want to add defined in variables, you can create a new custom object and then effectively, yet inefficiently, add it to the array.
$newOrganizationContext = [pscustomobject]#{
classificationId = $classificationId
group = $group
id = $id
isGroupSeparator = $isGroupSeparator
name = $name
objectId = $objectId
path = $path
subClass = $subClass
synchronized = $synchronized
type = $type
}
$test.result.organizationContext += $newOrganizationContext

Why does $DeploymentProcess.Steps.Name[0] return first if many, but first character of first if only one?

$ReleaseBody = #{
Id = $Project.Id
ProjectId = $Project.Id
ChannelId = $Channels.Items.Id
Version = $VERSION
SelectedPackages = #( #{
StepName = $DeploymentProcess.Steps.Name[0]
Version = $dt.Items.Version
})
} | ConvertTo-Json
I am querying an API that returns one or more StepName via $DeploymentProcess, I want to always select the first for my POST. The above works fine if there are multiple steps but not if there is only one. Thoughts?

Array of hashes not being passed by ref to sub

I have a setter sub setAssignmentStatus which takes an array of hashes (AoH from here on) and another parameter (do not concern yourself with this as that part works), and does something iterating through the AoH to set another entry in each hash element. It does not return anything because I want to use the same AoH object with the added entries after it is pulled through the setter sub and not construct a whole new AoH and repopulate the entries. Here is the setter:
sub setAssignmentStatus
{
my $fileFlatArySclr = $_[0];
my $cfgFile = $_[1];
#here I convert the AoH from the scalar necessary for the sub to its native form
my #fileFlatAry = #$fileFlatArySclr;
#this works, don't worry
my %cfgVarHash = getConfigVars($cfgFile);
foreach my $fileVarHashSclr(#fileFlatAry)
{
#convert each AoH entry from scalar necessary for iteration to native hash
my %varHash = %$fileVarHashSclr;
my $varName = $varHash{'VAR_NAME'};
my $asgnLineCnt = $varHash{'ASGN_CNT'};
my $asgnSts;
my $fileAsgnSts;
my $cfgAsgnSts;
if($asgnLineCnt > 0) { $fileAsgnSts = 1; } else { $fileAsgnSts = 0; }
my $cfgAsgnLine = $cfgVarHash{$varName};
if($cfgAsgnLine ne undef) { $cfgAsgnSts = 1; } else { $cfgAsgnSts = 0; }
$asgnSts = $fileAsgnSts.$cfgAsgnSts;
#debug to make sure $asgnSts is not null in the first place (it is not!)
print "\n*** setting ASGN_STUS of ".$varName." to ".$asgnSts;
#Here we set ASGN_STUS for every iteration
$varHash{'ASGN_STUS'} = $asgnSts;
}
}
It is called as follows:
setAssignmentStatus(\#fileFlatAry, $cfgFile);
However, after sending the #fileFlatAry AoH through setAssignmentStatus, each element hash does not contain an ASGN_STUS entry. Why is that and how can I fix it?
My suspicion is that I am doing something wrong with the \ modifier, which is how I am getting the data structure to be passed as a scalar parameter to the sub but I am not sure.
You modify %varHash instead of modyfing the referenced hash. Stop copying everything into local variables and modyfying the local variables.
$varHash{'ASGN_STUS'} = ...;
should be
$fileVarHashSclr->{'ASGN_STUS'} = ...;
I wouldn't do my #fileFlatAry = #$fileFlatArySclr; either. Pure waste.

MYSQL SELECT with 3 WHERE clause

I am having trouble with this code
$set = 'mycrazyemail#gmail.com'
$check_is_idk = $db->query("SELECT * FROM `ppl` WHERE `email` = '{$set}' AND `is` = 'idk' AND `belongs` = '{$id}'");
what is the problem?
You are missing a semi-colon at the end of the first line, it currently causes a parse error:
$set = 'mycrazyemail#gmail.com';
$check_is_idk = $db->query("SELECT * FROM `ppl` WHERE `email` = '{$set}' AND `is` = 'idk' AND `belongs` = '{$id}'");