String Concatenation Error Using Perls Dot Operator [duplicate] - string

This question already has an answer here:
Appending a string variable to a fixed string in Perl
(1 answer)
Closed 5 years ago.
I am trying to read input of two strings from the user keyboard, store them in two variables and concatenate the two strings together using the Perls dot operator.
Research I found online shows an example similar to what I am trying to accomplish. This example uses only one string variable in the concatenation but I think something similar should be able to concatenate multiple variables together:
$name = checkbook';
$filename = '/tmp/' . $name . '.tmp';
#$filename now contains "/tmp/checkbook.tmp"
(http://alvinalexander.com/perl/edu/articles/pl010003.shtml)
my code is displayed in the following - however, I am still getting the undesired concatenation :
$stringa=<STDIN>;
$stringb=<STDIN>;
print $stringa.$stringb;
compiled using perl (path)
output
nein
ja
nein
ja
instead of the desired output:
nein
ja
neinja
why am I not getting the concatenation output I think it should produce?

You can use "chomp" to remove the trailing string "\n", like this:
$stringa=<STDIN>;
$stringb=<STDIN>;
chomp($stringa);
chomp($stringb);
print $stringa.$stringb;

Related

Groovy replaceAll replacing multiple half matched strings

Working on a script where a user provides a file (yaml/json/txt) which has some string-formats to be replaced.These string formats can occur at any index.
The problem I'm facing is that replaceAll is replacing multiple half-matched strings. Example:
def secretfile = "This is first secret.adminlogin and then comes secret.adminloginpassword";
println(secretfile.replaceAll("secret.adminlogin", "root"));
This is resulting in "This is first root and then comes rootpassword" .
Where as the expected result is "This is first root and then comes secret.adminloginpassword"
How can we make groovy replaceAll to only look for exact match of string.
P.S , I cannot use split + replace + concatenation as the file can be a yaml/json and it might break it's linting.

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

string parts seperated by ; to ASCII written in a new string

Something like that is coming in:
str="Hello;this;is;a;text"
What I do want as result is this:
result="72:101:108:108:111;116:104:105:115;..."
which should be the Text in ASCII.
You could use string matching to get each word separated by ; and then convert, concat:
local str = "Hello;this;is;a;text"
for word in str:gmatch("[^;]+") do
ascii = table.pack(word:byte(1, -1))
local converted = table.concat(ascii, ":")
print(converted)
end
The output of the above code is:
72:101:108:108:111
116:104:105:115
105:115
97
116:101:120:116
I'll leave the rest of work to you. Hint: use table.concat.
Here is another approach, which exploits that fact that gsub accepts a table where it reads replacements:
T={}
for c=0,255 do
T[string.char(c)]=c..":"
end
T[";"]=";"
str="Hello;this;is;a;text"
result=str:gsub(".",T):gsub(":;",";")
print(result)
Another possibility:
function convert(s)
return (s:gsub('.',function (s)
if s == ';' then return s end
return s:byte()..':'
end)
:gsub(':;',';')
:gsub(':$',''))
end
print(convert 'Hello;this;is;a;text')
Finding certain character or string (such as ";") can be done by using string.find - https://www.lua.org/pil/20.1.html
Converting character to its ASCII code can be done by string.byte - https://www.lua.org/pil/20.html
What you need to do is build a new string using two functions mentioned above. If you need more string-based functions please visit official Lua site: https://www.lua.org/pil/contents.html
Okay...I got way further, but I can't find how to return a string made up of two seperate strings like
str=str1&" "&str2

How do I replace multiple words from string in nodejs? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
I would like to replace multiple words from below string:
\njava developer\n
How do I replace \n from start and \n from last from above string?
I used
replace('\n', '')
but it replace first \n only.
If you want to replace all \n without calling replace in a loop you have to use a regular expression. You can use it like this:
var test = "\njava developer\n";
var result = test.replace(/\n/g, '');
the g in the regular expression means replace all occurrences.
Hope this helps.
Thanks vincent!
It works for me.
I implement like this
stringToChange.toLowerCase().toString().replace(/[<b></b>\n]/g,'')
to replace
<\b>, <b> and \n
all multiple occurrences.

How do I replace a numeric part of a string by another number in Tcl?

I have a string in a format similar to 1005-abcd and I want to replace the numeric part of this string by another number and make it like 1008-abcd.
I can achieve this by using the following -
string map {1005 1008} "1005-abcd"
But I have these numbers in form of variables. For example, $source is 1005 and $new is 1008. When I use the same command like this -
string map {$source $new} "1005-abcd"
Braces prevent substitution of the variables. Use another way to give the list and enable substitution. One option:
string map "$source $new" "1005-abcd"
Another (better) option:
string map [list $source $new] "1005-abcd"

Resources