1

I have 2 arrays. If Joe Root is entered and exists, how can I print that array and the age "67" from the other array?

$myarr = ("John Morales", "Joe Root", "Peter Barkley" )
$myarr1 = (45, 67, 31)
$name = read-host "Enter your name" 

if ($myarr -contains $name)
{
     write-host "Name            Age"
     write-host "--------        ---"
     "{0,-11} {1,10}" -f "
 }
ZNKICE
  • 11
  • 2
  • 2
    `(45, 67, 31)[("John Morales", "Joe Root", "Peter Barkley").IndexOf('Joe Root')]`, but note that `.IndexOf` is case-sensitive. – mklement0 Mar 18 '22 at 20:42
  • 2
    In short: Use `$array.IndexOf()` for case-_sensitive_ string or non-string lookups, and `[Array]::FindIndex($array, [Predicate[object]] { 'foo' -eq $args[0] })` for case-_insensitive_ lookups. See the linked duplicate for details. – mklement0 Mar 18 '22 at 20:44
  • 1
    You should really look into [hash tables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7.2) – Santiago Squarzon Mar 18 '22 at 20:47

0 Answers0