Usage of \b in Perl [duplicate] - perl

This question already has answers here:
The "backspace" escape character '\b': unexpected behavior?
(5 answers)
Closed 7 years ago.
the \b "bakspace in perl doesn't works when we use it at the last of the string.
Eg: If we see the code, i have written
print "Hello\n";
print "Hello\n";
print "\bHe\bllo\b";
It gives me this output:
Hello Hello Hllo
So should the highlighted oo be deleted or in case, the \n would have been deleted taking the control to the 2nd line?

\b is a shorthand for \x08, so
print "a\b";
simply outputs bytes
61 08
Most terminals interpret 08 as a request to move the cursor one position to the left. If you want to "erase" a character, you need to overwrite it with another.
print "a\b \b";

Related

Can I save the split character of a string in Perl [duplicate]

This question already has answers here:
split() but keep delimiter
(2 answers)
Closed 11 months ago.
I want to split a multi sentence paragraph into its constituent sentences whilst retaining the split characters ie the '. ? !'. The code I'm using is:
my #Sentence = split(/[\.\?\!]/,$Paragraph);
Is there any way that I can save those sentence terminators?
Yes, if you add parentheses around the delimiter, they will be included in the result list.
my #Sentence = split /([\.\?\!])/, $Paragraph;
E.g. if you have the string foo.bar.baz before you would get qw(foo bar baz), and with parentheses you would get qw(foo . bar . baz).
In case you want to keep the delimiters attached to the sentence, you could use a lookbehind assertion
my #Sentence = split /(?<=[\.\?\!])/, $Paragraph;
# result qw(foo. bar. baz)
If you want to strip unnecessary spaces after the match, you could use /(?<=[\.\?\!]) */.

What does this line do in Perl? [duplicate]

This question already has answers here:
How to Replace white space in perl
(3 answers)
Closed 5 years ago.
What does this line do in Perl?
s/\s//g;
I'm looking at a script that is used to search and count certain characters in an input file and I understand everything in the code except for this line. I was wondering what this line did for the script?
s/\s//g;
is short for
$_ =~ s/\s//g;
It is a substitution operator bound to $_. It replaces all sequences in $_ that match the regex pattern \s with nothing. (Without g, it would only replace the first.)
\s matches a character of whitespace.

What are the matlab equivalent for '\n' '\t' and other escape characters? [duplicate]

This question already has answers here:
Escape characters in Matlab
(2 answers)
Closed 5 years ago.
I am reading a file using fileread() which returns me the entire file. Now I need to read line by line and convert them into process the data. Can I know how I would be able to detect the newline character in Matlab? I tried '\n' and '\r\n' and it doesn't work.
Thanks in advance
I already replied here (please avoid posting the question twice)Escape characters in Matlab
For special acharacters either use the char function with the character code (http://www.asciitable.com/) or sprintf (my preferred way for better readability. For example you are looking for sprintf('\n') or sprintf('\r\n')
char(13) is carriage return \r char(10) is new line \n

To check whether input line is word or sentence [duplicate]

This question already has an answer here:
Perl: find whether an particular element of array is a word or sentence
(1 answer)
Closed 9 years ago.
I have a line which can be a single word or sentence. What is the command line to check whether it is a single word or sentence ?
Your input is in $line.
Check like below
if(chomp($line) =~ /^\w+$/){
# only a word
} else {
# It contains multiple words
}
Coudln't you just check for spaces in the input line? If it contains a space it's safe to say it's a sentence? Then add some safety checks so it doesn't count when people write something like " word", "word ", etc. :)
do split(" ") and store in Array. If your array more than 1 element so it obviously not a word.

search some text in csv file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Search a value in CSV
I have created a perl script.
Lets suppose I want to seach "abc" in a CSV file which contains äbcd. the script that I have written shows me abcd as a output which actually I dont want, could anyone help on this.
The issue is searching for an ANSI string in unicode file. I think you can best answer your question by reviewing this regex tutorial that points out an example similar to yours.
http://www.regular-expressions.info/unicode.html
I guess you need to match exact word in perl:
use the below regex in perl:
(/\b"your_word"\b/
tested:
without \b
> echo 'abcd'|perl -lne 'if(/abc/){print}'
abcd
with \b
> echo 'abcd' | perl -lne 'if(/\babc\b/){print}'
>
> echo 'abc' | perl -lne 'if(/\babc\b/){print}'
abc
by looking at your code you are doing this:
(grep /$curr/, #nodes)
so changing it to :
(grep /\b$curr\b/, #nodes) should work