简体   繁体   English

如何将值从哈希表转换为其他列?

[英]How to transform values from a Hash Table to a different columns?

I'm new in Power Shell so I'm having a hard time to understand which command to use to a certain operation. 我是Power Shell的新手,所以我很难理解要对某个操作使用哪个命令。

PROBLEM: I have a hash table where every parameter can have 4 different words in a string that composes the field VALUE. 问题:我有一个哈希表,其中每个参数在组成字段VALUE的字符串中可以有4个不同的单词。 Eg.: 例如。:

$table:

Name                    Value
-----                   --------
parameter 1             F15 STK15                                                                                                                                                             
parameter 2             STK15
parameter 3             GP F15 STK15

As you can see, the table has 3 parameters and each parameter value is a STRING that could be formed by 3 different words: STK F15 and GP. 如您所见,该表具有3个参数,每个参数值都是一个STRING,可以由3个不同的词形成:STK F15和GP。

What I really need is to EXPORT this hash table to EXCEL in 4 different columns: 我真正需要的是将此哈希表导出到EXCEL中的4个不同的列中:

NAME    GP   STK15    F15

So in EXCEL there will be 4 columns: 因此,在EXCEL中将有4列:

NAME          GP    STK15    F15
parameter1    NULL  STK15    F15
and so on.

I don't know how to do it, piping with | 我不知道该怎么做。 out-GridView or what ever. out-GridView或其他。

If I could divide the string into 3 columns it already would be perfect. 如果我可以将字符串分成3列,那将是完美的。 can someone help me ? 有人能帮我吗 ?

You can do the following: 您可以执行以下操作:

$table = @{parameter1="F15 STK15";parameter2="STK15";parameter3="GP F15 STK15"}
$outArray = @()

foreach($entry in $table.GetEnumerator()) {
   $o = new-object PSCustomObject
   $o | Add-Member -notepropertyName "NAME" -notepropertyvalue $entry.name
   "F15","STK15","GP" | % { 
      if($entry.Value -match $_) {
        $o | Add-Member -notepropertyname "$_" -notepropertyvalue "$_"
      }
      else {
        $o | Add-Member -notepropertyname "$_" -notepropertyvalue "NULL"
      }
    }  
    $outArray += ,$o
}   

Iterate over the table and check for a match of each of the data you're trying to identify. 遍历表并检查要尝试标识的每个数据是否匹配。 Create a custom object for each value in the hashtable and populate it with the name, the required data and whether or not the each of the data were matched. 为哈希表中的每个值创建一个自定义对象,并使用名称,所需数据以及每个数据是否匹配来填充该对象。 Then append the object to an array. 然后将对象附加到数组。

You then have an object as such: 然后,您将得到一个这样的对象:

[PS] > $outArray | ft -auto

NAME       F15  STK15 GP
----       ---  ----- --
parameter2 NULL STK15 NULL
parameter1 F15  STK15 NULL
parameter3 F15  STK15 GP

And you can convert it to a csv format for exporting to Excel; 您可以将其转换为csv格式,以导出到Excel;

[PS] > $outArray | sort -Property NAME | ConvertTo-Csv -NoTypeInformation
"NAME","F15","STK15","GP"
"parameter1","F15","STK15","NULL"
"parameter2","NULL","STK15","NULL"
"parameter3","F15","STK15","GP"

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

相关问题 如何在Rust的哈希表中追加字符串值? - How to append to string values in a hash table in Rust? 如何用SQL中表的列中的值替换字符串 - How to replace a string with values from columns in a table in SQL 来自 hash map,如何将不同的值替换为输入语句中出现的每个“键”? - From a hash map, How to replace different values into each “key” that appeared in the input sentence? 如何在数据库表的不同列中插入字符串? - How to insert string into different columns in database table? 如何在Java中将字符串从txt文件转换为不同的子字符串 - How to transform Strings from a txt file to different substrings in java 如何在哈希表中哈希字符和整数字符串 - How to hash strings of chars and integers in hash table 使用unordered_set防止不同哈希值的键降落在同一存储桶中 - Preventing keys of different hash values from landing in same bucket with unordered_set Excel:如何求和与不同列中的某些字符串关联的值 - Excel: How to sum values associated with certain strings in different columns 如何将管道字符串拆分为另一个表中的不同列 - How to split a pipe string into different columns in another table 如何忽略 hash 表中的空格? - How to ignore spaces in an hash table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM