Cmdlets Hackerrank Solution — Powershell 3

$data = $inputLines[1] -split '\s+' | ForEach-Object [int]$_

He used Where-Object with the -match operator:

Where-Object  $_ -match '^\[ERROR\]' 

The ^ anchors to the start of the line, \[ escapes the bracket. powershell 3 cmdlets hackerrank solution

PowerShell 3 introduced -ReadCount with Get-Content, but for HackerRank, the simplest is:

$lines = Get-Content .\log.txt

But that loads the whole file. For huge logs, better is: $data = $inputLines[1] -split '\s+' | ForEach-Object [int]$_

Get-Content .\log.txt | ForEach-Object  ... 

He remembered: HackerRank test files are small, so either works.

You are given a list of processes or services. Your task is to: The ^ anchors to the start of the


$sum = 0
$arr | ForEach-Object  $sum += $_ 

| Component | Cmdlet/Syntax | Function | | :--- | :--- | :--- | | Input | Read-Host | Reads a line of input from the console user. | | Casting | [int] | Converts the string input into a 32-bit signed integer. This prevents string concatenation (e.g., "5" * 3 becoming "555" instead of 15). | | Arithmetic | * operator | Standard multiplication operator. | | Output | Write-Output | Sends the specified object (the calculated number) to the next command in the pipeline. If it is the last command, it writes to the console. |