Windows Vista Forums

Problems with foreach and array/hashtables
  1. #1


    Altraf Guest

    Problems with foreach and array/hashtables

    Hi All

    I'm struggling with something that seems simple, so what am I missing?

    I want to have an array (or hash) of hashtables, then to foreach through the
    array and pull out information. Here's an example:

    $a = @{}
    $a[1] = @{field1="a";field2="b"}
    $a[2] = @{field1="c";field2="d"}
    $a[3] = @{field1="e";field2="f"}

    I want to be able to:

    foreach ($b in $a) {
    "Field1: $($b.field1)"
    "Field2: $($b.field2)"
    "--------------------"
    }

    I desire:

    Field1: a
    Field2: b
    Field1: c
    Field2: d
    Field1: e
    Field2: f

    But I get:

    Field1:
    Field2:
    --------------------

    What am I doing wrong?

    Thanks.

    altraf



      My System SpecsSystem Spec

  2. #2


    Brandon Shell [MVP] Guest

    Re: Problems with foreach and array/hashtables

    When you foreach through a hash you need to either foreach throught he keys
    or the values.

    foreach($b in $a.keys){}
    or
    foreach($b in $a.values){}

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    A> Hi All
    A>
    A> I'm struggling with something that seems simple, so what am I
    A> missing?
    A>
    A> I want to have an array (or hash) of hashtables, then to foreach
    A> through the array and pull out information. Here's an example:
    A>
    A> $a = @{}
    A> $a[1] = @{field1="a";field2="b"}
    A> $a[2] = @{field1="c";field2="d"}
    A> $a[3] = @{field1="e";field2="f"}
    A> I want to be able to:
    A>
    A> foreach ($b in $a) {
    A> "Field1: $($b.field1)"
    A> "Field2: $($b.field2)"
    A> "--------------------"
    A> }
    A> I desire:
    A>
    A> Field1: a
    A> Field2: b
    A> Field1: c
    A> Field2: d
    A> Field1: e
    A> Field2: f
    A> But I get:
    A>
    A> Field1:
    A> Field2:
    A> --------------------
    A> What am I doing wrong?
    A>
    A> Thanks.
    A>
    A> altraf
    A>



      My System SpecsSystem Spec

  3. #3


    Shay Levi Guest

    Re: Problems with foreach and array/hashtables

    altraf,

    Use $a.Values

    PS > $a.Values

    Name Value
    ---- -----
    field1 e
    field2 f
    field1 c
    field2 d
    field1 a
    field2 b



    Another way is to use hashtables nested in array elements, I believe it gives
    the output you want:

    $a = @()
    $a+= @{field1="a";field2="b"}
    $a+= @{field1="c";field2="d"}
    $a+= @{field1="e";field2="f"}

    PS > $a

    Name Value
    ---- -----
    field1 a
    field2 b
    field1 c
    field2 d
    field1 e
    field2 f


    foreach($b in $a){
    "Field1: $($b.field1)"
    "Field2: $($b.field2)"
    }


    Field1: a
    Field2: b
    Field1: c
    Field2: d
    Field1: e
    Field2: f



    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com
    Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



    > Hi All
    >
    > I'm struggling with something that seems simple, so what am I missing?
    >
    > I want to have an array (or hash) of hashtables, then to foreach
    > through the array and pull out information. Here's an example:
    >
    > $a = @{}
    > $a[1] = @{field1="a";field2="b"}
    > $a[2] = @{field1="c";field2="d"}
    > $a[3] = @{field1="e";field2="f"}
    > I want to be able to:
    >
    > foreach ($b in $a) {
    > "Field1: $($b.field1)"
    > "Field2: $($b.field2)"
    > "--------------------"
    > }
    > I desire:
    >
    > Field1: a
    > Field2: b
    > Field1: c
    > Field2: d
    > Field1: e
    > Field2: f
    > But I get:
    >
    > Field1:
    > Field2:
    > --------------------
    > What am I doing wrong?
    >
    > Thanks.
    >
    > altraf
    >


      My System SpecsSystem Spec

  4. #4


    Oisin Grehan Guest

    Re: Problems with foreach and array/hashtables

    On Dec 10, 10:31 am, Altraf <Alt...@xxxxxx> wrote:

    > Thanks Brandon and Shay, I used foreach($b in $a.values){} and it worked great.
    >
    > altraf
    Altraf,

    Just so you know, Shay's script is subtly different - he's really
    creating an _array_ of hashtables. You, on the other hand, have
    created a hashtable of hashtables. The difference is:

    $a = @{} # curly braces, hashtable

    versus

    $a = @() # parentheses, array.

    - Oisin / x0n



      My System SpecsSystem Spec

  5. #5


    Altraf Guest

    Re: Problems with foreach and array/hashtables

    Oisin

    After a bit of reading up I became aware of the difference. Initially I was
    trying to output the identifier of the hashtables via a label, so my output
    would look like:

    Hash: 1
    Field1: a
    Field2: b
    Hash: 2
    Field1: c
    Field2: d
    Hash: 3
    Field1: e
    Field2: f

    Actually I don't really need the name, but was interested in how to get it,
    But never got it working. That's why I was trying to use a parent hashtable.

    As it happens Shay's way of doing it with a parent array instead of a parent
    hashtable proved slightly simpler, so I went with it.

    Thanks

    altraf

      My System SpecsSystem Spec

Problems with foreach and array/hashtables problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
hashtables ???? Chris PowerShell 2 22 Feb 2010
Trying to sort then FT an array of HashTables. Kevin Buchan PowerShell 10 20 Jan 2009
foreach, foreach-object & begin/process/end scriptblock clauses... Clint Bergman PowerShell 12 16 May 2007
Difference in semantics of for, foreach and foreach-object Andrew Watt [MVP] PowerShell 3 26 Jan 2007
possible bug with using the $foreach keyword inside foreach loops.. klumsy@gmail.com PowerShell 6 16 Nov 2006