Home > Mobile >  PowerShell: assign multiple values to variable, but later call only half?
PowerShell: assign multiple values to variable, but later call only half?

Time:05-23

Edit to make the question more simple

I'm essentially just trying to call only part of a variable that has multiple values.

example:

I have this variable

$a = "alocf", "arbmi", "ausbt", "auspg", "bmicw", "cidci", "cltcr", "cwnfl", "dkdsd", "dttsd", "elpca", "elyea", "grrgv", "grrym", "hbgno", "hopkn", "houra", "hourm", "housa", "irvbp", "jeftb", "lnhmd", "mcnbn", "mkcrd", "mkeav", "msnav", "msndd", "mspav", "myoca", "omacw", "omaha", "phxbh", "pitns", "rbkca", "rkcir", "rkswy", "rstev", "rstss", "rvrgr", "scfwi", "spiwx", "stlhb", "scvnm", "tmbpw", "tpafa", "wakny"

As you can see, it has multiple values, and eventually I'm trying to call that variable, but only call the first half of the available values.

Is there any way to do this?


Original question

So I'm rewriting a script for my work that is supposed to essentially open x amount of Google Chrome tabs for specific URLs, based on the number of people using it and which section they're doing. To sum that up, they run the script, it asks how many people are using it. Let's say 5 people are, then they have 5 choices to choose from, and those 5 choices distribute the 46 URL's between one another.

My issue is that I would like to make it so that it is more or less future proof. See, in my current one, for 5 people, I have 4 sections of 9 and 1 section of 10. However, if they update the URLs and all of the sudden there's 47, I'll need to change the entire thing pretty much, adjusting how many properties are within each section.

What my plan is, is basically I have a variable. It is set to 46 different reference words for each URL. I would love to be able to use an if statement such as:

#If statement for if 2 people are using script.
if ($b -eq "2") {

#Variable set equal to the total number of URL reference words($a.count) divided by 2, for if there are 2 people.  
$c = $a.count / 2

#Variable that subtracts the prior whole number of the previous variable, in case it is an odd number, from the total amount of URL reference words, to get the total number of the second half.
$d = $a.count - [Math]::Floor($c)

#Prompt to ask which section they're doing.
$e = Read-Host "Are you doing section 1 or section 2? >"

    #If statement for if they do the first half
    if ($e -eq "1") { }

}

It's obviously not finished, but that's the general setup for this part of the script. I would like to basically say, if they choose part 1, take the number that $c is equal to, and pull only that amount of URLs from a variable that has all the other variables.

I've looked around for the past couple of days, but apparently I'm either looking in the wrong place, or need to better my google searching skills lol.

Summed up: Is there a way to call only part of a variable, if that variable has multiple values?

Any help would be greatly appreciated, thank you.

CodePudding user response:

If I understood correctly, what you're looking to accomplish is to dynamically split an array into chunks, if my assumption is correct, you could use this helper function:

function ChunkArray {
    param([int] $Chunks)

    [ref] $ref = 0
    $toProcess = @($input)
    $groupSize = [math]::Ceiling($toProcess.Count / $Chunks)
    $tag = ''
    while($Chunks) {
        $Chunks = $Chunks / 10
        $tag  = 0
    }
    $toProcess | Group-Object {
        "Chunk{0:$tag}" -f [math]::Floor($ref.Value   / $groupSize)
    }
}

This function can take your array from pipeline using the automatic variable $input and split it / group it into the argument passed to the -Chunks parameter, for example, using your array $a from your question:

PS /> $chunks = $a | ChunkArray -Chunks 8
PS /> $chunks | Format-Table

Count Name         Group
----- ----         -----
    6 Chunk00      {alocf, arbmi, ausbt, auspg…}
    6 Chunk01      {cltcr, cwnfl, dkdsd, dttsd…}
    6 Chunk02      {grrgv, grrym, hbgno, hopkn…}
    6 Chunk03      {housa, irvbp, jeftb, lnhmd…}
    6 Chunk04      {mkeav, msnav, msndd, mspav…}
    6 Chunk05      {omaha, phxbh, pitns, rbkca…}
    6 Chunk06      {rstev, rstss, rvrgr, scfwi…}
    4 Chunk07      {scvnm, tmbpw, tpafa, wakny}

# Inspecting the 4th chunk:
PS /> $chunks[3].Group

housa
irvbp
jeftb
lnhmd
mcnbn
mkcrd
  • Related