[Solved] Perl search term [closed]

The following should work… $str = “sdfcatsdfdogffdfcatsdfjljlfflkfjflkjfdogsfsd”; @arr = $str =~ /(cat).{0,10}dog/sgi; print join(‘,’, @arr), “\n”; s to match over newlines, g to extract all matched instances, and i to ignore case. I’m not sure what you mean by ‘within 13’, but I’ve assumed here that as many as 10 characters can separate the ‘t’ … Read more

[Solved] Search file in directory structure

From one File::Find hater to another: DirWalk.pm, inspired by the Python’s os.walk(). package DirWalk; use strict; use warnings; sub new { my ($class, @dirs) = @_; my @odirs = @dirs; @dirs = qw/./ unless @dirs; s!/+$!! for @dirs; s!/+\.$!! for @dirs; my $self = { _odirs => [@odirs], _dirs => [@dirs], _dhstack => [], _dnstack … Read more

[Solved] How to randomly generate ANSI colors in Bash/Perl?

In bash, you need to use color escape sequences with echo -e random_colors.sh #!/bin/bash TXT=’the quick brown fox jumped over the lazy dog.’ WORDS=( $TXT ) for WORD in “${WORDS[@]}”; do let “i=$RANDOM % 256” echo -en “\e[38;5;${i}m$WORD \e[0m”; done; echo Running this 10 times: for i in `seq 1 10`; do bash random_colors.sh; done … Read more

[Solved] .= operator in perl

The dot (.) is the concatenation operator in Perl. $string = $a_substring . $another_substring; Sometimes you want to concatenate text to the same variable. $string = $string . $some_extra_text; Most binary operators in Perl have an “assignment” version which simplifies code like this. So instead of: $total = $total + $line_value; You can just write: … Read more

[Solved] Count matches between specific substrings

You can separate each target section of the string into an array using split. Then iterate through the array and do your count. my $string = ‘AAAAaaa>1BBbbbbbbb>2CCCCCCCCccccc>3DDDDDDDDDddd>4FFFFfffffff>’; my @targets = split(/(?=\d+\w+>)/, $string); my $successes = 0; foreach my $target (@targets){ my $target_lc = $target =~ tr/a-z//; my $target_uc = $target =~ tr/A-Z//; if($target_lc > $target_uc){ … Read more

[Solved] sed, awk, perl or lex: find strings by prefix+regex, ignoring rest of input [closed]

If you won’t have more than one of the pattern on a single line, I’d probably use sed: sed -n -e ‘s%.*https://\([-.0-9A-Za-z]\{1,\}\.[A-Za-z]\{2,\}\).*%\1%p’ Given the data file: Nothing here Before https://example.com after https://example.com and after Before you get to https://www.example.com And double your https://example.com for fun and happiness https://www.example.com in triplicate https://a.bb and nothing here The … Read more

[Solved] javascript Perl pack [closed]

pack ‘C’, pack ‘N’ and pack ‘H*’ are used to create a sequence of bytes. my $bytes = pack(‘C’, $uint8); # Array of bytes var bytes = []; bytes.push(uint8); # String of bytes var bytes = “”; bytes += String.fromCharCode(uint8); my $bytes = pack(‘N’, $uint32); # Array of bytes var bytes = []; bytes.push((uint32 >> … Read more

[Solved] calculating molecular weight in perl

first of all u must tell us what format has .fasta files. As i know they looks like >seq_ID_1 descriptions etc ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH ASDSADGDASHDASHSAREWAWGDASHASGASGASGSDGASDGDSAHSHAS SFASGDASGDSSDFDSFSDFSD >seq_ID_2 descriptions etc ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH ASDSADGDASHDASHSAREWAWGDASHASGASGASG if we will make suggestion that your code works fine, and counts molecular weight all we need is to read fasta files, parse them and count … Read more

[Solved] Perl to PHP equivalent: extract strings with regex

This code will do as you ask. It uses preg_match_all as simbabque described <?php $html=”<tr class=”aaa”><td class=”bbb”>221.86.2.163</td><td>443</td><td><div><span class=”ccc”></span> example <span> example</span></div></td></tr><tr class=”aaa”><td class=”bbb”>221.86.2.163</td><td>443</td><td><div><span class=”ccc”></span> example <span> example</span></div></td></tr>”; preg_match_all(‘|td class=”bbb”>([\d.]+)</td><td>(\d+)</td>|’, $html, $out, PREG_SET_ORDER); foreach ( $out as $item ) { echo “$item[1]:$item[2]\n”; } ?> output 221.86.2.163:443 221.86.2.163:443 9 solved Perl to PHP equivalent: extract strings with … Read more

[Solved] How do I create an XML file (in a specific location), containing a given hash? [closed]

XML::Simple is abysmal, especially for generating XML. You said the format should be the following: <keys> <key1><value1></value1></key1> […] </keys> That format doesn’t make much sense. The solution below produces XML in the following format: <elements> <element><key>key1</key><value>value1</value></element> […] </elements> Solution: use XML::Writer qw( ); open(my $fh, ‘>’, $qfn) or die(“Can’t create \”$qfn\”: $!\n”); my $writer = … Read more

[Solved] Perl Column comparison in two files

I’m going to guess you already have code for reading each of these files, and just need to parse out the values and organize them. The design part is coming up with a key that is unique for each start,end pair, but not hard to work with. Since start and end are numbers, this should … Read more