jeudi 30 août 2018

PowerShell hashtable storage for categorical data

I have data that is in the structure of: Subject > Category > Subcategories

e.g.

Planet of the Apes

-Scifi

--Movie

--TV series

-Popular

--Remake

--Cult Classic

----------------

BBC News

-Topical

--Daily News

-Geographical

--UK

--England

(the data isn't actually as bad this!)

I'm trying to work out the best way to convert this into something I can work with to filter and sort. Since the data currently is plain text, I've got an if statement that works out if it's a Subject, Category, or Subcategory but what would be the most sensible way to build a hashtable out of data like this?

$processedData = @{}
$versionattribs | ForEach-Object{
if($_ -match "^\s*$" -or $_ -match "Inherits.*")
    {
    # Is a blank line
    }
elseif($_ -notmatch "`t")
        {
        # Is a Subject
        Write-Host "Subject: $_ "
        $Subject = $_
        }
elseif($_ -match "`t" -and $_ -notmatch "`t`t")
        {
        # Is a category
        Write-host "Category: $_"
        $category = $_
        }
elseif($_ -match "`t`t")
        {
        # Is a sub-category label
        Write-Host "Label: $_ "
        $label = $_
        }
else
        {
        #Unexpected attribute
        Write-host "Error - unexpected line indentation : $_"
        }
}

Aucun commentaire:

Enregistrer un commentaire