使用csv文件中的数据在PowerShell中组合阵列和哈希表

时间:2016-03-12 14:49:26

标签: powershell powershell-v5.0

我正在尝试在PowerShell中为特定用例实现数据结构,其中数据以下列格式存储在CSV文件中

var childFV = document.getElementById('<%=ParentFormView.FindControl("ChildFormView1").ClientID  %>');
var childTextBoxes = childFV.getElementsByClassName('childTextBox');
var childCheckBoxes = childFV.getElementsByClassName('childCheckBox');

我需要解析csv以获得具有映射名称和参数集作为键值的复合hashmap。还需要访问所有参数的单个键值对。

我已经在java中成功实现了这一点,但不确定如何在PowerShell中解决这个问题。

1 个答案:

答案 0 :(得分:1)

这是你在想什么?它将遍历每一行的属性,对这些对进行分组,如果有值,则将它们添加到哈希表中。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/detail_frame_margin"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/movie_description"
        android:layout_alignParentTop="true"
        android:layout_marginRight="@dimen/detail_thumbnail_right_margin"
        android:layout_marginBottom="@dimen/detail_pic_bottom_margin"
        android:id="@+id/movie_thumbnail"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/movie_thumbnail"
        android:layout_marginBottom="@dimen/detail_data_margin_below"
        android:id="@+id/movie_name"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/movie_thumbnail"
        android:layout_below="@+id/movie_name"
        android:layout_marginBottom="@dimen/detail_data_margin_below"
        android:id="@+id/release_date"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/movie_thumbnail"
        android:layout_below="@+id/release_date"
        android:id="@+id/vote_average"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/movie_thumbnail"
        android:id="@+id/plot_synopsis"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/favorite"
        android:layout_toRightOf="@+id/movie_thumbnail"
        android:layout_below="@id/vote_average"
        android:background="@color/success_button"
        android:id="@+id/favourites_button" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/trailer_listview"
        android:layout_below="@+id/plot_synopsis"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/review_listview"
        android:layout_below="@+id/trailer_listview"/>


</RelativeLayout>

就个人而言,我会将哈希表存储在一个主哈希表中,该哈希表将$data = @" MappingName,Param1,Val1,Param2,Val2,Param3,Val3,Param4,Val4 map1,p11,v11,p12,v12,p13,v13,, map2,p21,v21,p22,v22,,,, map3,p31,v31,p32,v32,,,p34,v34 "@ | ConvertFrom-Csv #$data = Import-CSV -Path "mycsv.csv" $out = @() $data | ForEach-Object { #Hashtable for the parameters $ht = @{} #List properties $_.psobject.Properties | #Get only the grouped properties (that have a number at the end) Where-Object { $_.Name -match '\d+$' } | #Group properties by param/group number Group-Object {$_.Name -replace '\D+(\d+)$', '$1' } | ForEach-Object { $param = $_.Group | Where-Object { $_.Name -match 'param' } $value = $_.Group | Where-Object { $_.Name -match 'val' } #If property has value if($value.value -ne ""){ #Add to hashtable $ht.add($param.Value,$value.Value) } } $ht.add("MappingName",$_.MappingName) $out += $ht } $out Name Value ---- ----- p13 v13 p11 v11 p12 v12 MappingName map1 p21 v21 p22 v22 MappingName map2 p32 v32 p34 v34 MappingName map3 p31 v31 $out[0] Name Value ---- ----- p13 v13 p11 v11 p12 v12 MappingName map1 作为键,参数的哈希表作为值。例如:

MappingName