简体   繁体   English

根据另一个数组获取数组

[英]Get array based on another array

I need to take a CSV file and then create many files out of that in different formats depending on the account number. 我需要获取一个CSV文件,然后根据帐号以不同的格式创建许多文件。 The account numbers I need are in other files with no other fields (I need to pad them to match). 我需要的帐号在没有其他字段的其他文件中(我需要填充它们以匹配)。 This is just one below. 这只是下面的一个。

I know I can cycle through using foreach, but was trying to avoid that. 我知道我可以循环使用foreach,但是正试图避免这种情况。 This gives no error, but doesn't work even though I know there are matches. 这没有错误,但是即使我知道有匹配项也无法正常工作。

$sms_accts = import-csv -path "H:\scrap\Positive Pay Feeds\sms_accounts.txt"
$input_file = import-csv -path "H:\scrap\Positive Pay Feeds\CNVDC939.POSITIVE PAY ISSUES.txt"

$sms_accts = $sms_accts  | Select-Object @{Name="Acct";Expression={$_.Acct.PadLeft(11,"0")}}

$to_sms_file = $input_file  | where-object { $sms_accts -contains ($_.ACCOUNT_NUMBER.PadLeft(11,"0"))  }

$to_sms_file

It returns nothing. 它什么也不返回。

Sample file below. 下面的示例文件。

sms_accounts.txt: sms_accounts.txt:

Acct
12345678
23456789


Positive Pay:
    "SPONSOR_BANK_ID","BE_CLIENT_ID","COMPANY_NAME","ACCOUNT_NUMBER","SERIAL","AMOUNT","PAYEE","ISSUE_TYPE","STATUS","ENTRY_DATE","ISSUE_DATE"
    "100741","1004928","Acme","0012345678","11111","2468","","0","1","2/11/2014 5:50:34 PM","2/10/2014"
    "100741","1004928","ABC","009999999","22222","180.34","","0","1","2/20/2014 9:01:38 PM","2/20/2014"

You can use Acct property to filter $sms_accts 您可以使用Acct属性过滤$sms_accts

... | where-object { $sms_accts.Acct -contains ($_.ACCOUNT_NUMBER.PadLeft(11,"0"))}

in this way you don't need to use foreach in your code. 这样,您无需在代码中使用foreach

You process $sms_accts into an array of custom objects with the single property Acct : 您将$sms_accts处理$sms_accts具有单个属性Acct自定义对象数组:

$sms_accts = $sms_accts  | Select-Object @{Name="Acct";Expression={$_.Acct.PadLeft(11,"0")}}

but then check the array for a particular string : 但是然后检查数组中的特定字符串

... | where-object { $sms_accts -contains ($_.ACCOUNT_NUMBER.PadLeft(11,"0")) }

Since none of your custom objects is a string (even though each has a string property), you don't get a match. 由于您的所有自定义对象都不是字符串(即使每个自定义对象都具有string属性),因此您不会获得匹配项。 Change the two lines 更改两行

$sms_accts = import-csv -path "H:\scrap\Positive Pay Feeds\sms_accounts.txt"

and

$sms_accts = $sms_accts  | Select-Object @{Name="Acct";Expression={$_.Acct.PadLeft(11,"0")}}

into this: 到这个:

$sms_accts = Import-Csv "H:\scrap\Positive Pay Feeds\sms_accounts.txt" | % {
               $_.Acct.PadLeft(11,"0")
             }

and your check will work as expected. 您的支票将会按预期运作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM