Perl Split Operator [closed] - perl

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 months ago.
Improve this question
I have a large text file of records each beginning with 22-. I want to read in the file and split it up into an array with one record per element. Looks like I will lose the 22- at the beginning of each element. Is there a way to split it without losing the 22-? I suppose after the split I could add the 22- back to the beginning of each element.
I haven't coded it yet

With split, you specify the separator. 22- is not a separator, but part of the record.
You can still use split by separating on the zero-width space that's followed by 22-.
split /(?=22-)/

Related

Find and replace multiple string in MATLAB [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a1 in the following form in MATLAB.
a1{1,1} = {'x1','x2','x3'}
a1{1,2} = {'x1','x2','x3','x4'}
a1{1,3} = {'x4'}
I need to replace
'x1' with 'Text1'
'x2' with 'Text2'
'x3' with 'Text3'
'x4' with 'Text4'
I have tried a couple of things with no success and I am a bit lost, does anyone have any ideas.
Thanks
Regular Expressions might be a way to go with this
a1{1,1} = {'x1','x2','x3','x4'};
a1{1,1} = regexprep(a1{1,1},'x([0-9])','text$1');
Will result in a1{1,1} containing
{'text1'} {'text2'} {'text3'} {'text4'}
This will work for any single digit numerical value (0-9) that is prefixed with the letter x.

unable to make table in markdown format [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
https://github.com/ishan-nitj/Competitve-Programming/blob/master/TODO%20PROBLEMS.md
I have made a table in md format but it is not displaying at github.However same is working at a markdown editor:link
From Github directly : Organizing information with tables
There must be at least three hyphens in each column of the header row.
Your first header row only has one hyphen.
You also have to put an empty line on top of your table, like this :
Date:17 September 2016
|SNO|LEVEL|NAME|COMMENTS|DONE ON|
|---|---------------|--------------|----------|----|
1|DIV2 E|[Pashmak and Graph|(http://codeforces.com/problemset/problem/459/E)|Tried but getting WA
Result :

Sed: print 2 lines before a match [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to print the two previous lines before matches inside a file (for any match)
How to make it?
Thank you
The script:
sed -n "1N;2N;/XXX[^\n]*$/{h;s/\n[^\n]*$//;p;g};N;D"
works as follows:
Read the first three lines into the pattern space, 1N;2N
Search for the test string XXX in the last line, and if found: save pattern space in hold space h, delete last line s, print p, and then restore saved string g
Append the next line input to pattern space, N
Delete first line from pattern space and restart cycle, D, noting that 1N;2N is no longer applicable
See also similar SED: addressing two lines before match.
If you do not insist on using sed, use
grep -B 2

Comments are getting executed when I run the Perl Script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Some of my perl statements are executed even after commenting. I tried all the delimiter #, /*..*/, //
Does anyone face the same issue or can anyone help me how to solve this issue?
Your question appears to be "How do I create a comment in Perl?"
perlsyn says thusly:
Text from a "#" character until the end of the line is a comment, and is ignored. Exceptions include "#" inside a string or regular expression.
For example,
print "apple\n"; # Keeps the doctor away.
Keep in mind that comments can only be used where whitespace is expected. For example, the following does not contain any comments since the # is part of the string literal.
print "apple # Keeps the doctor away.
orange
";

Search for keyword and then conditional on finding it search for another keyword from a list of words using Perl [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
My question concerning Perl is very similar to the one involving Python answered here:
How can I search within a document for a keyword and then subsequent key words within a set number of lines of the original keyword in Python?
This involves searching for one keyword and then for another from a list of keywords (say you have a list of 4 words) in the vicinity of the original keyword (say plus minus 5 lines) and then when matched printing out a range of lines (say plus minus 20 lines from the original keyword)
Two extensions if possible:
Ext 1: in the printout highlight key words
Ext 2: search all files from a directory and add the filename to the printout (filename in one line, followed by the lines extracted from the file)
I would appreciate even if directed to similar examples...
Read in the File as a nice array:
my #line = <FILE>;
Search for the 1st word:
my $i;
foreach $i (0 .. $#line) {
last if $line[$i] =~ /$firstWord/i;
}
Get the surrounding using an array slice:
my #surrounding = #line[$i-5 .. $i+5];
One should however use some index checking like
[0<=$i-5 ? 0 : $i-5 .. $i+5<#line ? $i+5 : $#line]
repeat
Profit
The rest is left as an exercise to the reader.
For highlighting write
$line[$i] =~ s{($firstWord)}{<strong>$1</strong>};
or similar, depending on your output medium. map is your friend for this.