- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Generating Random Letters
Hi,
I have a script that opens data set where I have a list of "Operator". I have to manually assign each person a random letter in the "Analyst" but wondering if there is a code that could loop though all the different operators and assign a random letter to the analyst column?
Thanks!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Generating Random Letters
Here's one way. Note that 'RandomShuffle()' assures you don't assign the same letter more than once (which I assume is what you need):
NamesDefaultToHere(1);
// List of possible values
letterList = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
// Number of rows in data table
n = 10;
// Values to assign to new character column
colVals = letterList[RandomShuffle(1::n)];
Print(colVals);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Generating Random Letters
One option would be to use associative array in combination with @ian_jmp solution. Something like this could work:
Names Default To Here(1);
dt = New Table("test",
addrows(100),
New Column("Operator", character, set values({"ABC", "ABC", "DEF", "GHI"}));
);
// List of possible values
letterList = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
uniq_oper = Associative Array(dt:Operator) << get keys;
Remove From(uniq_oper, Contains(uniq_oper, "")); // remove missing
uniq_oper_letter = letterList[Random Shuffle(1::N Items(letterList))][1::N Items(uniq_oper)];
oper_aa = Associative Array(uniq_oper, uniq_oper_letter);
dt << New Column("Analyst", character, << Set Each Value(
If(Contains(oper_aa, :Operator),
oper_aa[:Operator]
,
""
)
));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Generating Random Letters
Here's one way. Note that 'RandomShuffle()' assures you don't assign the same letter more than once (which I assume is what you need):
NamesDefaultToHere(1);
// List of possible values
letterList = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
// Number of rows in data table
n = 10;
// Values to assign to new character column
colVals = letterList[RandomShuffle(1::n)];
Print(colVals);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Generating Random Letters
Thanks, how do I account for the same operator name. I would have multiple rows where initials are "ABC" so I want all of these to be the same random letter?
NamesDefaultToHere(1);
dt=new table();
dt<<addrows(100);
dt<<new column("Operator", character, set values ({"ABC", "ABC", "DEF", "GHI"}));
// List of possible values
letterList = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
// Number of rows in data table
n = 10;
// Values to assign to new character column
colVals = letterList[RandomShuffle(1::n)];
Print(colVals);
dt<<new column ("Analyst", character,set values(colVals));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Generating Random Letters
One option would be to use associative array in combination with @ian_jmp solution. Something like this could work:
Names Default To Here(1);
dt = New Table("test",
addrows(100),
New Column("Operator", character, set values({"ABC", "ABC", "DEF", "GHI"}));
);
// List of possible values
letterList = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
uniq_oper = Associative Array(dt:Operator) << get keys;
Remove From(uniq_oper, Contains(uniq_oper, "")); // remove missing
uniq_oper_letter = letterList[Random Shuffle(1::N Items(letterList))][1::N Items(uniq_oper)];
oper_aa = Associative Array(uniq_oper, uniq_oper_letter);
dt << New Column("Analyst", character, << Set Each Value(
If(Contains(oper_aa, :Operator),
oper_aa[:Operator]
,
""
)
));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content