在严格模式版本2下,通过组对象的密钥获取哈希表值?

时间:2015-09-30 06:09:51

标签: powershell

以下脚本(按xy转动数组列表)不起作用。 ($hashvariable.x无效)。如何改写呢?在严格模式下,在哈希表中通过键获取值似乎并不容易。

Set-StrictMode -version 2 # change 2 to 1 will work

$a = @('a','b','x',10), 
     @('a','b','y',20), 
     @('c','e','x',50), 
     @('c','e','y',30)

$a | %{ 
    new-object PsObject -prop @{"label" = "'$($_[0])','$($_[1])'"; value=@{ $_[2]=$_[3]}} 
} | 
group label | % {
    "$($_.Name), $($_.Group.value.x), $($_.Group.value.y)" # error
    #"$($_.Name), $($_.Group.value['x']), $($_.Group.value['y'])" # empty for x,y
} 

预期结果。

'a','b', 10, 20
'c','e', 50, 30

错误:

Property 'x' cannot be found on this object. Make sure that it exists.
At line:6 char:35
+     "[$(@($_.Name -split ",") + @($_.Group.value.x, $_.Group.value.y))]"
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

2 个答案:

答案 0 :(得分:1)

很难理解这个脚本实际上是什么,无论如何,问题是只有集合中的第一个元素$ _.Group.Value具有属性' x',同时time,strict模式禁止引用对象的不存在的属性,因此您会收到此错误。

这是一个解决方案,希望我能够理解你想要的东西:

$a = @('a','b','x',10), 
     @('a','b','y',20), 
     @('c','e','x',50), 
     @('c','e','y',30)

$hashtable = @{}

$a |%{
    $hashtable["$($_[0]) $($_[1])"] += 
        [hashtable]@{$_[2] = $_[3]}
}

$hashtable.GetEnumerator() | %{
    "$($_.Key) $($_.Value['x']) $($_.Value['y'])"   
}

答案 1 :(得分:1)

不确定你真正想要什么,但这是我最好的猜测。您应该在单个 public class CSVToExcelConverter extends AsyncTask<String, Void, Boolean> { private final ProgressDialog dialog = new ProgressDialog(DatabaseExampleActivity.this); @Override protected void onPreExecute() {this.dialog.setMessage("Exporting to excel..."); this.dialog.show();} @Override protected Boolean doInBackground(String... params) { ArrayList arList=null; ArrayList al=null; //File dbFile= new File(getDatabasePath("database_name").toString()); File dbFile=getDatabasePath("database_name"); String yes= dbFile.getAbsolutePath(); String inFilePath = Environment.getExternalStorageDirectory().toString()+"/excerDB.csv"; outFilePath = Environment.getExternalStorageDirectory().toString()+"/test.xls"; String thisLine; int count=0; try { FileInputStream fis = new FileInputStream(inFilePath); DataInputStream myInput = new DataInputStream(fis); int i=0; arList = new ArrayList(); while ((thisLine = myInput.readLine()) != null) { al = new ArrayList(); String strar[] = thisLine.split(","); for(int j=0;j<strar.length;j++) { al.add(strar[j]); } arList.add(al); System.out.println(); i++; }} catch (Exception e) { System.out.println("shit"); } try { HSSFWorkbook hwb = new HSSFWorkbook(); HSSFSheet sheet = hwb.createSheet("new sheet"); for(int k=0;k<arList.size();k++) { ArrayList ardata = (ArrayList)arList.get(k); HSSFRow row = sheet.createRow((short) 0+k); for(int p=0;p<ardata.size();p++) { HSSFCell cell = row.createCell((short) p); String data = ardata.get(p).toString(); if(data.startsWith("=")){ cell.setCellType(Cell.CELL_TYPE_STRING); data=data.replaceAll("\"", ""); data=data.replaceAll("=", ""); cell.setCellValue(data); }else if(data.startsWith("\"")){ data=data.replaceAll("\"", ""); cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(data); }else{ data=data.replaceAll("\"", ""); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(data); } //*/ // cell.setCellValue(ardata.get(p).toString()); } System.out.println(); } FileOutputStream fileOut = new FileOutputStream(outFilePath); hwb.write(fileOut); fileOut.close(); System.out.println("Your excel file has been generated"); } catch ( Exception ex ) { ex.printStackTrace(); } //main method ends return true; } protected void onPostExecute(final Boolean success) { if (this.dialog.isShowing()) { this.dialog.dismiss(); } if (success) { Toast.makeText(DatabaseExampleActivity.this, "file is built!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(DatabaseExampleActivity.this, "file fail to build", Toast.LENGTH_SHORT).show(); } } } 对象中累积一个组中的所有对象,而不是为每个输入对象创建单独的Hashtable

Hashtable