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

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.

Related

Perl Split Operator [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 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-)/

How can i make github search to not show multiple results with the same file name? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
I have a problem where when i search something in github, there are something 1000 results that are all literally the same file, all the same name, and they are not forks either.
Basically these are just copy pasta codes, and for example i get 1000 results that all end up in xxx.c, which all contain the same code used in different projects..
My question is, is it possible to limit github to only find unique file names? So in our example, only show 1 result that has xxx.c at the end.
Not really, from my experience: once you have speficied your criteria from "Searching Code", any file (from different non-fork repositories) would be displayed.
Even though they might be the same name/content.

What does lexicographically mean? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I saw a program in codeforces where it says, "Now Petya wants to compare those two strings lexicographically."
I didn't understand it. What does lexicographically mean?
I'm not sure if this is more of a stackoverflow question or not, but I could be wrong. . . Based on your question and its usage I would prefer a breakdown of the word "lexicographically".
The root appears to be "lexicon", which we can see by this reference the definition is:
"a book containing an alphabetical arrangement of the words in a language and their definitions"
OR
"the vocabulary of a language, an individual speaker or group of speakers, or a subject"
OR (more related to computers)
"the total stock of morphemes in a language"
In this pdf the author says,
"The lexicon of a computer language is its total inventory of words and symbols."
Next, we want to look at the word "lexicography", this is the next layer to our process of building the word up from the root. First, let us look at the general definition. According to Merriam-Webster in this reference we see that lexicography is defined as such:
"the editing or making of a dictionary"
OR
"the principles and practices of dictionary making"
In this reference regarding Computation Lexicography the author states, "Computational Lexicology is the use of computers in the study of the lexicon. It has been more narrowly described by others (Amsler, 1980) as the use of computers in the study of machine-readable dictionaries."
The next-to-last step is to look at the word without its description of the action occurring, that would be "lexicographic", as in lexicographic order. In this reference we see, "In mathematics, the lexicographic or lexicographical order (also known as lexical order, or dictionary order) is a generalization of the alphabetical order of the dictionaries to sequences of ordered symbols or, more generally, of elements of a totally ordered set."
Lastly, we can see that the word is an adverb since in the example sentence it is describing the action that is occurring - also we see the use of "-ly" at the end of the word.
It would appear that Petya would like to compare the two strings alphabetically and with regard to their potentially symbolic, or definitive, meaning within the dictionary of lexicons from which they exist.
A fun little analysis project that took me on.

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
";