Octave adds spaces with strcat
In Octave I run these commands:
strcat ("hel", " ", "lo")
I get this result:
ans = hello
Instead of what I expected:
ans = hel lo
strcat to me sounds like "concatenate strings". A space is a valid character, so adding a space should be OK. Matlab has the same behaviour, so it's probably intended.
I find it counter intuitive. Does this behavior makes sense?
Hmm. It works how it is defined:
"strcat removes trailing white space in the arguments (except within cell arrays), while cstrcat leaves white space untouched. "
From http://www.gnu.org/software/octave/doc/interpreter/Concatenating-Strings.html
So the question could be: Should this behaviour be changed.
strcat takes the input parameters and trims the trailing spaces, but not the leading spaces. if you pass a parameter as one or more spaces, they are collapsed to blank string.
That behavior is a manifestation of how "cellstr" works where spaces at the end are removed.
Work around 1
If you put the space up against the 'lo', it is a leading space and not removed.
strcat ("hel", " lo")
ans = hel lo
Work around 2
use cstrcat instead:
cstrcat("hel", " ", "lo")
ans = hel lo
Work around 3
Use sprintf, can be faster than strcat.
sprintf("%s%s%s\n", "hel", " ", "lo")
ans = hel lo
Related
I have a string that is like so:
"string content here
"
because it is too long to fit on the screen in one line
The string is the name of a file i would like to read, but i always get an error message that the file name wasn't found because it includes the new line character in the string when this obviously isn't in the file name. I cannot rename the file and I have tried the strip function to remove it, but this doesn't work. How can I remove the enter character from my string so I can load my file?
You can use the function strip to remove any trailing whitespace from a string.
>> text = "hello" + newline; %Create test string.
>> disp(text)
hello
>> text_stripped = strip(text);
>> disp(text_stripped)
hello
>>
In the above ">>" has been included to better present the removal of the whitespace in the string.
Consider replacing the newline character with nothing using strrep. Link
As an example:
s = sprintf('abc\ndef') % Create a string s with a newline character in the middle
s = strrep(s, newline, '') % Replace newline with nothing
Alternatively, you could use regular expressions if there are several characters causing you issues.
Alternatively, you could use strip if you know the newline always occurs at the beginning or end.
I have an assignment asking me to print x iterations of a string for each character in that string. So if the string input is "Gum", then it should print out:
Gum
Gum
Gum
Right now my code is
my $string = <>;
my $length = length($string);
print ($string x $length, "\n");
And I'm getting gum printed five times as my output.
Those who have said you will get CR + LF at the end of the line on a Windows system are mistaken. Perl will convert the native line ending to a simple newline \n on any platform.
You must bear this in mind whether you are reading from the terminal or from a file.
The built-in chomp function will remove the line terminator character from the end of a string variable. If the string doesn't end with a line terminator then it will have no effect.
So when you type GumEnter you are setting $string to "Gum\n", and length will show that it has four characters.
You are seeing it five times on your screen because the first line is what you typed in yourself. The following four are printed by the program.
After a chomp, $string is just "Gum" with a length of three characters, which is what you want.
To output this on separate lines you have to print a newline after each line, so you can write
my $string = <>;
chomp $string;
my $length = length $string;
print ("$string\n" x $length);
or perhaps
print $string, "\n" for 1 .. $length;
I hope that helps
As you are simply using the input string, it still contains the newline at the end. This is also counted as a character. On my system, it outputs 4 Gum\n.
chomp($string) will remove the line ending, but the output will then also run together, resulting in GumGumGum\n
When You insert input and press enter afterwards You don't enter "Gum" but "Gum\r\n" which is a string of length 5. You should do trimming.
Your code is working fine. See this: http://ideone.com/AsPFh3
Possibility 1: It might be that you're putting 2 spaces while giving input from command line, that's why the length comes out to be 5, and it prints 5 times. Something like this: http://ideone.com/fsvnrd
In above case the my $string=<>; will give you my $string = "gum "; so length will be 5.
Possibility 2:
Another possibility is that if you use Windows then it will add carriage return (\r) and new line (due to enter \n) at the end of string. So it makes the length 5.
Edit: To print in new line: Use the below code.
#!/usr/bin/perl
# your code goes here
chomp(my $string=<>);
my $length = length($string);
print ("$string\n" x $length);
Demo
Edit 2: To remove \r\n use the below:
$string=~ s/\r|\n//g; Read more here.
I was helping someone today regex some info out of a pdf file that we read in as a txt file. Unfortunately the tm packages readPDF function was not working correctly at the time, though through a few modifications we were able to get it to work just fine. While we were regexing out some of the fluff from the .txt file we found something that was surprising to most of us, namely that the string "\040" gets interpreted as a space, " ".
> x <- "\040"
> x
> [1] " "
This doesn't happen for other, similar character strings (i.e. "\n" or "\t") that you may expect this to happen for.
> y <- "\n"
> y
> [1] "\n"
> z <- "\t"
> z
>[1] "\t"
Why is this? What other character strings are interpreted differently in R?
EDIT:
It seems after simple experimentation, any "\xxx" where x are digits yields a different result. What is the value of this?
Take a look here: http://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html
Backslash is used to start an escape sequence inside character constants. Escaping a character not in the following table is an error.
...
\nnn character with given octal code (1, 2 or 3 digits)
Then take a look at this ASCII table to see how octal codes get represented. As you will see 040 is a space.
And just for fun:
> '\110\145\154\154\157\040\127\157\162\154\144\041'
[1] "Hello World!"
How do I do a VIM search/replace with the following conditions:
a) the line contains str1
b) the line also contain str2
c) the line do NOT contain str3
on such a line, I do want to replace xxx with yyy
This will replace the () string in every line of a file containing the string return and the string aLib at some point after that:
:%s/return.*aLib.*\zs()\ze/(aHandle)/
The % is the operating range of the command, which includes all lines in the file.
\zs denotes the start of the replaced string and \ze denotes its end.
If you have to replace more than one instance per line, put a g at the end of the command after the last /. If you wish to confirm each substitution, put the c flag at the end.
By assuming the () string is never present if aHandle is present, this command doesn't answer your question exactly, but based on the sample provided in the comments, this may match your needs.
regex doesn't handle and, not very well. there are ways to do it, but bit tricky. A very simple function could solve your problem:
function! Rep()
let l = getline('.')
if stridx(l, 'str1')>=0 && stridx(l,'str2') >=0 && stridx(l,'str3')<0
execute 's/xxx/yyy/g'
endif
endfunction
you can
either write it in your .vimrc file.
or save it in a foo.vim file and open it with vim type :so %
then open your target file, type :%call Rep() you will see for example:
str1 str2 str3 xxx
str3 str2 xxx
str2 xxx
*str1 str2 xxx
*xxx str2 xxx str2 xxx str1
would be changed into:
str1 str2 str3 xxx
str3 str2 xxx
str2 xxx
*str1 str2 yyy
*yyy str2 yyy str2 yyy str1
I think replacing str1,str2,xxx,yyy to your real values in that function isn't hard for you, is it?
EDIT
your real problem seems to be much easier than the problem you described in question. try this line:
:%s/\(\s*return\s*aLib\.[^(]\+(\)\s*\()\)/\1aHandle\2/
Note that it could be written shorter only for your example working, but I want it to be secure.
I think #Kent's answer (the function) is the "most correct" when doing complex searches/substitutions. It's easier to change afterward, and to understand it also after a while, it's also quicker to write than this. But I think it can be interesting if it's possible to do this with a "oneliner".
I welcome comments about this not working, I find it interesting myself if it's possible, not if it's the "best solution".
A "oneliner" for all situations described in the original question.
:v/str3/s/\v(.*str1&.*str2)#=.{-}\zsxxx/yyy/g
Explanation:
:v/str3/ all lines NOT containing "str3"
s/ substitute
\v "very magic" (to not have to escape regex expressions like {}), :h \v
(.*str1&.*str2)#= positive look-ahead, :h \#=
.*str1 any character 0 or more ("greedy") followed by "str1", the preceding .* is important for both & and #= to work
& pattern before and after & must be present, different than | that is using "or", both patterns must start at same place, thats why .* is used before str1/2, :h \&
#= the atom before (in the parentheses) should be present, but the pattern is not included in the match, the atom must however start at the beginning of the line for this to work (.* makes sure of that).
.{-} any character 0 or more ("not greedy"), {-} instead of * is important if more than one occurrence of xxx should be substituted (not enough with the g flag in this case)
\zs sets the start of the match (what to substitute).
xxx finally, the string to substitute
/yyy/ replace the match with yyy
g all occurences on the line
This gives the same result as using the example in #Kent's answer, it will handle any order of str1, str2, str3 and xxx.
I repeat that this is an alternative if for some reason a function is not an alternative. As I understand OP, e.g. str2 is always after str1 in his case, so this is more a general solution for situations like this (and perhaps more of academic interest).
Escape characters cause a lot of trouble in R, as evidenced by previous questions:
Change the values in a column
Can R paste() output "\"?
Replacing escaped double quotes by double quotes in R
How to gsub('%', '\%', ... in R?
Many of these previous questions could be simplified to special cases of "How can I get \ out of my way?"
Is there a simple way to do this?
For example, I can find no arguments to gsub that will remove all escapes from the following:
test <- c('\01', '\\001')
The difficulty here is that "\1", although it's printed with two glyphs, is actually, in R's view a single character. And in fact, it's the very same character as "\001" and "\01":
nchar("\1")
# [1] 1
nchar("\001")
# [1] 1
identical("\1", "\001")
# [1] TRUE
So, you can in general remove all backslashes with something like this:
(test <- c("\\hi\\", "\n", "\t", "\\1", "\1", "\01", "\001"))
# [1] "\\hi\\" "\n" "\t" "\\1" "\001" "\001" "\001"
eval(parse(text=gsub("\\", "", deparse(test), fixed=TRUE)))
# [1] "hi" "n" "t" "1" "001" "001" "001"
But, as you can see, "\1", "\01", and \001" will all be rendered as 001, (since to R they are all just different names for "\001").
EDIT: For more on the use of "\" in escape sequences, and on the great variety of characters that can be represented using them (including the disallowed nul string mentioned by Joshua Ulrich in a comment above), see this section of the R language definition.
I just faced the same issue - if you want any \x where x is a character then I am not sure how, I wish I knew, but to fix it for a specific escape sequence,. say \n then you can do
new = gsub("\n","",old,fixed=T)
in my case, I only had \n