windows - Powershell not splitting string returned from function -
i trying import csv contains fictional places , hours each day of week fictional places open.
the hours 5:00-4:00 format. have spaces. created function remove spaces. after function run, appears powershell can't run further operations on returned string (i.e. -split).
the csv:
node,sat,sun,mon,tue,wed,thu,fri pizzaplace,9:00 – 4:30,0,8:00-3:30,7:00 – 10:00,10:00 – 4:00,10:00 – 4:00,10:00 – 4:00 bigpharma,0,5:00 – 4:00,7:00-6:00,7:00-6:00,0,0,7:00-6:00 greenhouse,12:00-8:00,0,12:00-7:30,12:00-7:30,12:00-7:30,12:00-7:30,12:00-7:30 portapoty,12:00-8:00,closed,10:00-6:00,10:00-7:30,10:00-6:00,10:00-7:30,10:00-6:00
the ps1 script:
function unk-ampm ($openstr) { $openstr -replace " "; } $csvinputs = import-csv samphours.csv; $srprbn = "our hours for"; $srpran = "are"; $dswed = "wednesday"; foreach ($csvline in $csvinputs) { $retailer = $csvline.node; [string] $openwed = unk-ampm $csvline.wed; write-host "value of openwed before split: "$openwed; $openweda = $openwed -split "-"; write-host "value of openweda[0]: "$openweda[0]; write-host "value of openweda[1]: "$openweda[1]; if ($openweda[0] -eq 0 -or $openweda[0] -eq 'closed') { $ohswed = "closed"; } else { $ohswed = $openweda[0] + " " + $openweda[1]; } write-host $srprbn $retailer $srpran $ohswed "on" $dswed; }
and results:
value of openwed before split: 10:00–4:00 value of openweda[0]: 10:00–4:00 value of openweda[1]: our hours pizzaplace 10:00–4:00 on wednesday value of openwed before split: 0 value of openweda[0]: 0 value of openweda[1]: our hours bigpharma closed on wednesday value of openwed before split: 12:00-7:30 value of openweda[0]: 12:00 value of openweda[1]: 7:30 our hours greenhouse 12:00 7:30 on wednesday value of openwed before split: 10:00-6:00 value of openweda[0]: 10:00 value of openweda[1]: 6:00 our hours portapoty 10:00 6:00 on wednesday
the –
in entries spaces not hyphens (u+002d
), dashes (u+2013
)
you'll see if data has been copied text processor "automagical" formatting (like word example).
use "punctuation, dash" unicode category match , split both kinds:
$openstr -split '\p{pd}'
Comments
Post a Comment