Cannot access the array values outside for loop or foreach

Hi,

  I am  trying to access the array d1 or d2 outside foreach or for loop and all I get is the null array. I checked online regarding functions etc. but not sure how exactly to access the array values outside the for loop or for each loop. The array elements are displayed correctly when called inside the loop but nothing if called outside the loop. Below is the code:

=============

$Path = "C:\Scripting\text3"
$Path1 = Get-Content $Path
$Text = "Accounting-Input-Octets:"
$Text2 = "Accounting-Output-Octets:"
$Text3 = "Rating-Group:"
$Text4 = "Subscription-Id-Data:"
$Text5 = "Event-Timestamp:"
$Text6 = "Time-First-Usage:"
$Text7 = "Time-Last-Usage:"
$Text8 = "AF-Charging-Identifier:"
$Text9 = "PS-Information:"
$Text10 = "Stop-Time:"
$d1 = @()


$b = @()|Format-table -autosize

$mdn =  $Path1 | Select-String -Pattern "Subscription-Id-Data:"

   write-output $mdn[0,1,2,3,4,5]

   write-output $Path1[0]

 
 
    
    ForEach ($p in $Path1)  {

      if ($p -eq $mdn[0])
        
      { $c = $p; write-output "value of c is" $c  }

      if ($c -eq $mdn[0])

           {

              $d1 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '

             
            
             
              }

             # write-output $sum1

            if ($p -eq $mdn[1])
        
      { $c = $p; write-output "value of c is" $c  }
      
      if ($c -eq $mdn[1])

           {

              $d2 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '   

               

        }
        
        
        
        
        

        if ($p -eq $mdn[2])
        
      { $c = $p; write-output "value of c is" $c  }
        
        
        if ($c -eq $mdn[2])

           {

              $d3 = ($p | Select-String -Pattern "Accounting-Input-Octets:")  -replace 'Accounting-Input-Octets:',' '

              }

              
             
              
             

         write-output $d1  
         write-output $d2
        write-output $d3
       
        
      }
      
       write-output $e1
      
      
      

    
   # write-output $array.$d1
      
      

     # write-output Sum($d1)

     

September 1st, 2015 1:41pm

Can you please post you code so that it is readable.  It is just a mess of unrelated lines.

I recommend placing trace statements in your loop to see if it is working.

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 2:06pm

Hi. Not sure, but I can read the code from here. Copied it again, please let me know if it is still not readable.

Thanks,

Amit

$Path = "C:\Scripting\text3"
$Path1 = Get-Content $Path
$Text = "Accounting-Input-Octets:"
$Text2 = "Accounting-Output-Octets:"
$Text3 = "Rating-Group:"
$Text4 = "Subscription-Id-Data:"
$Text5 = "Event-Timestamp:"
$Text6 = "Time-First-Usage:"
$Text7 = "Time-Last-Usage:"
$Text8 = "AF-Charging-Identifier:"
$Text9 = "PS-Information:"
$Text10 = "Stop-Time:"
$d1 = @()


$b = @()|Format-table -autosize

$mdn =  $Path1 | Select-String -Pattern "Subscription-Id-Data:"

   write-output $mdn[0,1,2,3,4,5]

   write-output $Path1[0]




    ForEach ($p in $Path1)  {


      if ($p -eq $mdn[0])

      { $c = $p; write-output "value of c is" $c  }

      if ($c -eq $mdn[0])

           {

              $d1 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '




              }

             # write-output $sum1

            if ($p -eq $mdn[1])

      { $c = $p; write-output "value of c is" $c  }

      if ($c -eq $mdn[1])

           {

              $d2 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '   



        }






        if ($p -eq $mdn[2])

      { $c = $p; write-output "value of c is" $c  }


        if ($c -eq $mdn[2])

           {

              $d3 = ($p | Select-String -Pattern "Accounting-Input-Octets:")  -replace 'Accounting-Input-Octets:',' '

              }






         write-output $d1  
         write-output $d2
         write-output $d3


      }

       write-output $d1





   # write-output $array.$d1



     # write-output Sum($d1)

                                                                                                                                                                                                                                                                    
September 1st, 2015 2:11pm

Please post code in code posting control.  like this:

foreach($x in $y){
    #do something
}
foreach($x in $y){
    #do something
}

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 2:23pm

OK, sending in the code posting control


ForEach ($p in $Path1)  {


      if ($p -eq $mdn[0])

      { $c = $p; write-output "value of c is" $c  }

      if ($c -eq $mdn[0])

           {

              $d1 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '

           }
              }
write-output "$d1" < ------------- Null display


September 1st, 2015 2:59pm

Full code

$Path = "C:\Scripting\text3"
$Path1 = Get-Content $Path
$Text = "Accounting-Input-Octets:"
$Text2 = "Accounting-Output-Octets:"
$Text3 = "Rating-Group:"
$Text4 = "Subscription-Id-Data:"
$Text5 = "Event-Timestamp:"
$Text6 = "Time-First-Usage:"
$Text7 = "Time-Last-Usage:"
$Text8 = "AF-Charging-Identifier:"
$Text9 = "PS-Information:"
$Text10 = "Stop-Time:"
$d11 = @()


$b = @()|Format-table -autosize

$mdn =  $Path1 | Select-String -Pattern "Subscription-Id-Data:"

   write-output $mdn[0,1,2,3,4,5]

   write-output $Path1[0]

  
  
    
    ForEach ($p in $Path1)  {

    
      if ($p -eq $mdn[0])
        
      { $c = $p; write-output "value of c is" $c  }

      if ($c -eq $mdn[0])

           {

              $d1 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '

             [Array]$d11+=[Array]$d1
            
             
              }

             # write-output $sum1

            if ($p -eq $mdn[1])
        
      { $c = $p; write-output "value of c is" $c  }
      
      if ($c -eq $mdn[1])

           {

              $d2 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '   

               

        }
        
        
        
        
        

        if ($p -eq $mdn[2])
        
      { $c = $p; write-output "value of c is" $c  }
        
        
        if ($c -eq $mdn[2])

           {

              $d3 = ($p | Select-String -Pattern "Accounting-Input-Octets:")  -replace 'Accounting-Input-Octets:',' '

              }

              
             
              
             

         write-output $d1  
         write-output $d2 
         write-output $d3
       
        
      }
      
       write-output $d11
      
      $sum=0

      for ($i=0;$i -lt $d11.length; $i++) {

         $sum+=$d11[$i]

         }

         write-host "Total Accounting Input Octets for:"$mdn[0] " is $sum"
      
      
    
   # write-output $array.$d1 
      
      

     # write-output Sum($d1)

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 3:00pm

.

What is it that you are trying to do?

There is really no way to understand your script.  It seems to be trying to load something from a file and then it reads the file contents in a loop.

Put trace statements in to find out what is happening.  I suspect that most of your variables are blank or do not meet the criteria of you logic.

September 1st, 2015 3:22pm

OK thanks. I got the below output.

After I put [Array]$d11+=[Array]$d1 inside foreach statement, I was able to access the Array d11 outside the foreach loop.

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 3:32pm

$Path = "C:\Scripting\text3"
$Path1 = Get-Content $Path
$Text = "Accounting-Input-Octets:"
$Text2 = "Accounting-Output-Octets:"
$Text3 = "Rating-Group:"
$Text4 = "Subscription-Id-Data:"
$Text5 = "Event-Timestamp:"
$Text6 = "Time-First-Usage:"
$Text7 = "Time-Last-Usage:"
$Text8 = "AF-Charging-Identifier:"
$Text9 = "PS-Information:"
$Text10 = "Stop-Time:"
$d11 = @()
$d21 = @()
$d31 = @()


$b = @()|Format-table -autosize

$mdn =  $Path1 | Select-String -Pattern "Subscription-Id-Data:"

   write-output $mdn[0,1,2,3,4,5]

   write-output $Path1[0]

  
  
    
    ForEach ($p in $Path1)  {

    
      if ($p -eq $mdn[0])
        
      { $c = $p; write-output "value of c is" $c  }

      if ($c -eq $mdn[0])

           {

              $d1 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '

             [Array]$d11+=[Array]$d1
            
             
              }

             # write-output $sum1

            if ($p -eq $mdn[1])
        
      { $c = $p; write-output "value of c is" $c  }
      
      if ($c -eq $mdn[1])

           {

              $d2 = ($p | Select-String -Pattern "Accounting-Input-Octets:") -replace 'Accounting-Input-Octets:',' '   

               [Array]$d21+=[Array]$d2

        }
        
        
        
        
        

        if ($p -eq $mdn[2])
        
      { $c = $p; write-output "value of c is" $c  }
        
        
        if ($c -eq $mdn[2])

           {

              $d3 = ($p | Select-String -Pattern "Accounting-Input-Octets:")  -replace 'Accounting-Input-Octets:',' '

              [Array]$d31+=[Array]$d3

              }

              
             
              
             

         write-output $d1  
         write-output $d2 
         write-output $d3
       
        
      }
      
       write-output $d11
      
      $sum1=0

      for ($i=0;$i -lt $d11.length; $i++) {

         $sum1+=$d11[$i]

         }

         write-host "Total Accounting Input Octets for:"$mdn[0] " is $sum1"
      
      $sum2=0

      for ($i=0;$i -lt $d21.length; $i++) {

         $sum2+=$d21[$i]

         }

         write-host "Total Accounting Input Octets for:"$mdn[1] " is $sum2"


         $sum3=0

      for ($i=0;$i -lt $d21.length; $i++) {

         $sum3+=$d31[$i]

         }

         write-host "Total Accounting Input Octets for:"$mdn[2] " is $sum3"

    
   # write-output $array.$d1 
      
      

     # write-output Sum($d1)

      

September 1st, 2015 3:33pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics