powershell - Reading strings from text files using switch -regex returns null element -


question:

the intention of script filter out name , phone number both text files , add them hash table name being key , phone number being value.

the problem facing $name = $_.current returning $null, result of hash not getting populated.

can tell me issue is?

contents of file1.txt:

lori

234 east 2nd street

raleigh nc 12345

9199617621

lori@hotmail.com

=================

contents of file2.txt:

robert

2531 10th avenue

seattle wa 93413

2068869421

robert@hotmail.com

sample code:

$hash = @{}  switch -regex (get-content -path c:\users\svats\desktop\fil*.txt)  {  '^[a-z]+$' { $name = $_.current}   '^\d{10}'  {             $phone = $_.current          $hash.add($name,$phone)          $name=$phone=$null        } default        { write-host "nothing matched"        }  } $hash 

remove current property reference $_:

$hash = @{}  switch -regex (get-content -path c:\users\svats\desktop\fil*.txt) {     '^[a-z]+$' {          $name = $_     }       '^\d{10}'  {         $phone = $_         $hash.add($name, $phone)         $name = $phone = $null     }     default {         write-host "nothing matched"     }  } $hash 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -