1

I am trying to import a CSV which contains fictional places and hours each day of the week those fictional places are open.

The hours are from 5:00-4:00 format. Some have spaces. I created a function to remove the spaces. After that function is run, it appears PowerShell can't run any further operations on the 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] + " to " + $openWedA[1]; }

    Write-Host $srPRBN $retailer $srPRAN $ohsWed "on" $dsWed;

}

And the results:

Value of openWed before split:  10:00–4:00
Value of openWedA[0]:  10:00–4:00
Value of openWedA[1]:  
Our hours for PizzaPlace are 10:00–4:00 to  on Wednesday

Value of openWed before split:  0
Value of openWedA[0]:  0
Value of openWedA[1]:  
Our hours for BigPharma are 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 for GreenHouse are 12:00 to 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 for Portapoty are 10:00 to 6:00 on Wednesday
3
  • I don't see any issues? Do you mean the second record with 0? Thats because the second record has a 0 in the wednsday column... Commented Nov 17, 2016 at 17:34
  • 1
    He means the first result with 10:00-4:00. It should have been split so that $openWedA contained two elements with each time, but it seems like it didn't work on that one whereas it did on the others. Commented Nov 17, 2016 at 17:35
  • Yeahhh, I totaly missed that. Commented Nov 17, 2016 at 17:57

1 Answer 1

4

The in the entries with spaces are not hyphens (U+002D), but dashes (U+2013)

You'll usually see this if the data has been copied from a text processor with "automagical" formatting (like Word for example).

Use the "Punctuation, dash" unicode category to match and split both kinds:

$openStr -split '\p{Pd}'
Sign up to request clarification or add additional context in comments.

1 Comment

The function you modified just removes spaces. I think you meant to put that regex in the -split. But I would also include \s in the -split regex and get rid of that function altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.