Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Test if array in one line

I do a get-content on a file. Sometimes there are a lot of lines, but it can happen there is only one line (or even 0)

I was doing something like

$csv = (gc $FileIn)
$lastID = $csv[0].Split(' ')[1] #First Line,2nd column

But with only one line, gc return a string and $csv[0] return the first caracter of the string instead of the complete line, and the following Split fail.

Is it possible to do something like :

$lastID = (is_array($csv)?$csv[0]:$csv).Split(' ')[1]

And to do that only if $csv contains at least a line?

Thx for your help, Tim

like image 947
timmalos Avatar asked Aug 16 '13 07:08

timmalos


People also ask

How do you check if an array contains a value PowerShell?

Use the Contains method from an array. In the following example, the $array variable contains an array. The next line checks to see if the number 2 is in the array. It is, and the method returns True.

How do I compare two arrays in PowerShell?

You can also use PowerShell to compare arrays using the Compare-Object cmdlet. This cmdlet takes a reference object and a difference object and returns a side indicator indicating which elements are and are not in either array. You can see below that the Compare-Object cmdlet allows you to compare both arrays at once.

What is @() in PowerShell?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.

How do I declare a string array in PowerShell?

PowerShell array of strings is the collection of string objects that is multiple strings are residing into the same collection which can be declared using String[], @() or the ArrayList and can be used in various ways like functions, in the file content, as a variable and can perform the multiple operations on the ...


2 Answers

There are type operators one can use to test the type of a variable. -is is the one you need. Like so,

$foo = @()        # Array
$bar = "zof"      # String
$foo -is [array]  # Is foo an array?
True              # Yes it is
$foo -is [string] # Is foo a string?
False             # No it is not
$bar -is [array]  # How about bar
False             # Nope, not an array
$bar -is [string] # A string then?
True              # You betcha!

So something like this could beused

if($csv -is [array]) {
    # Stuff for array
} else {
    # Stuff for string
}
like image 174
vonPryz Avatar answered Oct 06 '22 00:10

vonPryz


Instead of doing:

$csv = (gc $FileIn)

you had to

$csv = @(gc $FileIn)

Now the output will always be an array of strings irrespective of the file having one line or not. The rest of the code will just have to treat $csv as an array of strings. This way is better than having to check if the output is an array etc., at the least in this situation.

like image 29
manojlds Avatar answered Oct 06 '22 00:10

manojlds