Select List Element Programmatically Using Name Stored as String

select column names containing string programmatically

We can do

df %>% 
select_at(vars(matches(paste(searchfor, collapse="|")))) %>%
select(order(sub(".*_", "", names(.))))

Programmatically setting a list identifier

First, you will have to create a List where T is the Type of objects contained in the list.

So, your code should read as

private List<T> myList;

From the javadoc,

<T> the type of elements in this list.

In your case, the compiler will not know the type of elements that is to be added to your list and hence it won't work.

However you can use List<Object> as an alternate, but that is a generally code-smell in long run and very difficult to maintain

  • It kills the idea of generics.
  • Makes your code prone to ClassCastException.
  • In a perfect world and even if you are safe while adding elements,
    you will need to suppress warning everywhere and need to cast back to
    the type.

Proper Solution : You can write an interface, which all your objects of the ArrayList will adhere to. Proceed with something like

List<'YOUR INTERFACE TYPE HERE'> myList = new ArrayList<>();

Don't you need to worry of the object type inside the list. Hope this helps!

If you are too specific to save in String, here is a workaround.

public static <T> List<T> getCastedList(List<Object> objList, Class<T> clazz) {
List<T> newList = new ArrayList<T>();
if (objList != null) {
for (Object object : objList) {
if (object != null && clazz.isAssignableFrom(object.getClass())) {
newList.add((T) object);
}
}
}
return newList;
}

And call this as

getCastedList(myList, Class.forName(VARIABLE));

How do I programmatically set the value of a select box element using JavaScript?

You can use this function:

function selectElement(id, valueToSelect) {    
let element = document.getElementById(id);
element.value = valueToSelect;
}

selectElement('leaveCode', '11');
<select id="leaveCode" name="leaveCode">
<option value="10">Annual Leave</option>
<option value="11">Medical Leave</option>
<option value="14">Long Service</option>
<option value="17">Leave Without Pay</option>
</select>

Creating empty lists with the name of the elements of another list

Here you go, it uses the function exec to execute the command as if it were typed by you but you use i as a dynamic variable name

for i in my_list:
exec(i+'=[]')

Keep in mind this isn't very safe to do

Dynamically select data frame columns using $ and a character value

You can't do that kind of subsetting with $. In the source code (R/src/main/subset.c) it states:

/*The $ subset operator.

We need to be sure to only evaluate the first argument.

The second will be a symbol that needs to be matched, not evaluated.

*/

Second argument? What?! You have to realise that $, like everything else in R, (including for instance ( , + , ^ etc) is a function, that takes arguments and is evaluated. df$V1 could be rewritten as

`$`(df , V1)

or indeed

`$`(df , "V1")

But...

`$`(df , paste0("V1") )

...for instance will never work, nor will anything else that must first be evaluated in the second argument. You may only pass a string which is never evaluated.

Instead use [ (or [[ if you want to extract only a single column as a vector).

For example,

var <- "mpg"
#Doesn't work
mtcars$var
#These both work, but note that what they return is different
# the first is a vector, the second is a data.frame
mtcars[[var]]
mtcars[var]

You can perform the ordering without loops, using do.call to construct the call to order. Here is a reproducible example below:

#  set seed for reproducibility
set.seed(123)
df <- data.frame( col1 = sample(5,10,repl=T) , col2 = sample(5,10,repl=T) , col3 = sample(5,10,repl=T) )

# We want to sort by 'col3' then by 'col1'
sort_list <- c("col3","col1")

# Use 'do.call' to call order. Seccond argument in do.call is a list of arguments
# to pass to the first argument, in this case 'order'.
# Since a data.frame is really a list, we just subset the data.frame
# according to the columns we want to sort in, in that order
df[ do.call( order , df[ , match( sort_list , names(df) ) ] ) , ]

col1 col2 col3
10 3 5 1
9 3 2 2
7 3 2 3
8 5 1 3
6 1 5 4
3 3 4 4
2 4 3 4
5 5 1 4
1 2 5 5
4 5 3 5

Variable name as a string in Javascript

Typically, you would use a hash table for a situation where you want to map a name to some value, and be able to retrieve both.

var obj = { myFirstName: 'John' };obj.foo = 'Another name';for(key in obj)    console.log(key + ': ' + obj[key]);


Related Topics



Leave a reply



Submit