substitution

All possible combinations of selected character substitution in a string in ruby

允我心安 提交于 2019-12-29 07:14:16
问题 I was wondering if there is a simple way to do every combination of selected character substitutions in ruby in a simple way. An example: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs.combination.each { |c| string.gsub c } would yield "this is @ test" "th!s !s a test" "thi$ i$ a te$t" "th!s !s @ test" "thi$ i$ @ te$t" "th!$ !$ a te$t" "th!$ !$ @ te$t" Thanks for the help! 回答1: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs = subs.first.map(&:to_a) 1

How to preserve spaces when outputting a shell variable? [duplicate]

不问归期 提交于 2019-12-29 01:59:08
问题 This question already has answers here : I just assigned a variable, but echo $variable shows something else (7 answers) Closed 2 years ago . For string matching purposes I need to define a bash variable with leading spaces. I need to define this starting from an integer, like: jj=5 printf seems to me a good idea, so if I want to fill spaces up to 6 character: jpat=`printf " %6i" $jj` but unluckly when I am trying to recall the variable: echo $jpat the leading whitespaces are removed and I

Multiple, specific, regex substitutions in Python

孤街浪徒 提交于 2019-12-25 10:55:09
问题 What I would like to do is to make specific substitions in a given text. For example, '<' should be changed to '[', '>' to ']', and so forth. It is similar to the solution given here: How can I do multiple substitutions using regex in python?, which is import re def multiple_replace(dict, text): # Create a regular expression from the dictionary keys regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys()))) # For each match, look-up corresponding value in dictionary return regex.sub

Simultaneous changing of python numpy array elements

最后都变了- 提交于 2019-12-25 08:48:11
问题 I have a vector of integers from range [0,3], for example: v = [0,0,1,2,1,3, 0,3,0,2,1,1,0,2,0,3,2,1] . I know that I can replace a specific values of elements in the vector by other value using the following v[v == 0] = 5 which changes all appearences of 0 in vector v to value 5 . But I would like to do something a little bit different - I want to change all values of 0 (let's call them target values ) to 1 , and all values different from 0 to 0 , thus I want to obtain the following: v = [1

Substituting values of a matrix

冷暖自知 提交于 2019-12-24 10:49:30
问题 Say I have the following two matrices: >> x = [1 4 3; 6 4 3; 6 9 3; 2 4 3; 5 4 0; 5 3 1; 6 4 7]; >> y = [0 0 1; 1 1 0; 1 1 0; 0 1 1; 0.2 0.8 0.54; 1 1 1; 0 0 0]; Where you can think of x as some image, and y the degree of membership of of each element of x to some region of interest. Say I set those elements in x that have degree of membership = 1 to 1 and the other elements to 0 as follows: x = zeros(size(y)); x(y==1) = 1; In which case I will have the following output: 0 0 1 1 1 0 1 1 0 0 1

Bash variable substitution and strings

丶灬走出姿态 提交于 2019-12-24 03:00:12
问题 Let's say I have two variables: a="AAA" b="BBB" I read a string from a file. This string is the following: str='$a $b' How to create a new string from the first one that substitutes the variables? newstr="AAA BBB" 回答1: bash variable indirection whithout eval : Well, as eval is evil , we may try to make this whithout them, by using indirection in variable names. a="AAA" b="BBB" str='$a $b' newstr=() for cnt in $str ;do [ "${cnt:0:1}" == '$' ] && cnt=${cnt:1} && cnt=${!cnt} newstr+=($cnt) done

Is $1 or $& faster for replacing a matched string using s/// in Perl?

一曲冷凌霜 提交于 2019-12-24 01:44:14
问题 Which one of these is cheaper? $_ = 'abc123def'; s/\d+/$&*2/e; say; s/(\d+)/$1*2/e; say; 回答1: Executive summary : use 5.010's /p instead. The performance of $& is about the same for a single match or substitution, but the entire program can suffer from it. It's slowdown is long-range, not local. Here's a benchmark with 5.010, which I suspect you are using since you have say in there. Note that 5.010 has a new /p flag that supplies a ${^MATCH} variable that acts like $& but for only one

How do I set LINESIZE and PAGESIZE with a substitution variable?

懵懂的女人 提交于 2019-12-24 00:54:59
问题 I am trying to use a PL/SQL script in SQL*Plus to set the linesize and pagesize based on a developer input on whether a report should print in landscape or portrait orientation. If it is landscape, I want linesize 132 and pagesize 60. If it is portrait, I want 88 and 80 respectively. I am trying to use substitution variables to do so like this: DEFINE PRINT_ORIENTATION 'PORTRAIT' COLUMN LINESIZE_VALUE NOPRINT new_value LINE COLUMN PAGESIZE_VALUE NOPRINT new_value PAGE SELECT DECODE('&PRINT

Reassigning strings to a set list of values in an array

元气小坏坏 提交于 2019-12-23 20:39:27
问题 I have a list in Lua which is fixed format (it's an input from somewhere else). E.g. a = {"apple", "apple 1", "pear", "orange", "orange 3", "kiwi", "tomato"} I also have a look up table where I want to normalise this set and send them to a code format. Note: anything containing apple, such as apple 1, apple 2 etc will be mapped to the same value as apple. E.g. "apple" => "RD" "pear" => "GR" "orange" => "OG" "kiwi" => "GR" "tomato" => "RD" "banana" => "YL" etc... I then want to return a list

How can I replace all the text before the match in a Perl substitution?

大兔子大兔子 提交于 2019-12-23 20:17:19
问题 I am reading each line of an input file (IN) and printing the line read to an output file (OUT) if the line begins with one of the patterns, say "ab", "cd","ef","gh","ij" etc. The line printed is of form "pattern: 100" or form "pattern: 100:200". I need to replace "pattern" with "myPattern", i.e. print the current line to FILE but replace all the text before the first occurence of ":" with "myPattern". What is the best way to do this? Currently I have: while ( <IN> ) { print FILE if /^ab:|^bc