How to Append a String to a Variable That Either Exists or Not

how to append a string to a variable that either exists or not?

I would probably do it like this:

@results = @results.to_s + "run"

This works because NilClass defines a #to_s method that returns a zero-length String, and because instance variables are automatically initialized to nil.

Appending string to variable name

The right was is to use either an array or an object, depending on if the id is going to be sequential or named.

var card_links_grid = {};
var card_id = "the_hanged_man";
card_links_grid[card_id] = "some value";

or

var card_links_grid = [];
card_links_grid.push("some value");

Variable variables are possible, but only with some hard to maintain and debug approaches that should be avoided. Use object types designed for the data structure you want to represent.

Append suffix to string only if it is present (or not nil)


string.presence&.concat('suffix')

In case you care for nils only and not overall presence, you can simplify it:

string&.concat('suffix')

If you don't want to mutate the original value, you can instead do the somewhat cryptic:

string&.+('suffix')

perl: append variable to string if it exists

To shift a variable from @_ and insert it, if it's there

print $var1 . (shift // '') . $var2;

For example, with a space inserted

sub concat {
my ($var1, $var2) = ("Hello", "Friend");
print $var1 . (shift // ' ') . $var2, "\n";
}

concat();
concat(" my dear ");

There may be questions of design with spaces, please change as suitable. This prints


Hello Friend
Hello my dear Friend

If the angle brackets need be inserted around the variable, with $var2 already declared

print "$var1 " . (defined $var2 ? "<$var2>" : '') . "$var3\n";

In a sub, and without declaring an extra variable

sub concat {
my ($var1, $var2) = ("Hello", "Friend");
print $var1 . (@_ ? ' <' . join(' ', @_) . '> ' : ' ') . $var2, "\n";
}

concat();
concat("my dear");
concat("my", "very", "dear");

Prints

Hello Friend
Hello <my dear> Friend
Hello <my very dear> Friend

Again, change the handling of spaces as suitable for the application.


You can also use substr conditionally on an existing string but that is more cumbersome.

Append to string variable

Like this:

var str = 'blah blah blah';
str += ' blah';

str += ' ' + 'and some more blah';

how to concat string with dynamic value if that value exists

Try this:





var numb = 12;

var msg = "Hello x" + (numb > 5 ? (', ' + numb + ' is your promo code') : '');

console.log(msg);

Apparently not able to append a string to another

Be aware that all strings are filled with trailing blanks (space characters) after their last non-space character. This is very important!

'a' // ' ' really produces  'a '

but

result = result // ' '

produces a 2001 character string (you are appending to the whole 2000-character result including the trailing blanks), which is then truncated on assignment, so that result ends up being the same.

You may want

result = trim(result) // ' '

but it is also useless, because the string is filled with trailing blanks (spaces) anyway.

It would make sense when you append something non-blank:

  character(4) :: str

str = "a"
str = trim(str) // "bcd"
print *, str
end

It should print abcd.


If you want to make the variable larger, you have to use:

character(:), allocatable:: result
result = 'a' !now contains 'a' and has length 1
result = result // 'b' !now contains 'ab' and has length 2

It works also with space characters:

character(:), allocatable:: result
result = ' ' !now contains ' ' and has length 1
result = result // ' ' !now contains ' ' and has length 2

(In old versions of Intel Fortran one had to enable reallocation on assignment for this behaviour.)

How do you append to an already existing string?

In classic sh, you have to do something like:

s=test1
s="${s}test2"

(there are lots of variations on that theme, like s="$s""test2")

In bash, you can use +=:

s=test1
s+=test2


Related Topics



Leave a reply



Submit