Hi Monks, I will be extremely thankful if the monks here could help me understanding regex in Perl. Suppose I have a text file that has these lines: This is line one. Line Two is this. Third line starts here. This is line four. This is line five. This is line six. This is the seventh line. This is line eight. [download] If the record contains the word "third" our "four", I want it to print the whole record, meaning the stuff between the empty lines, not just lines with those words in it. But I am not able to write it correctly. Here is what I have tried: use strict; use warnings; open my $fh, "+<", "testlines.txt"; while (<$fh>) { if ($_=~ /(third | four)/si) { chomp; local $/ = "



"; print "line is: $_

"; } } [download] And it prints: >perl regex.pl line is: Third line starts here. line is: This is line four. [download] But what I want is: This is line one. Line Two is this. Third line starts here. This is line four [download] Where am I going wrong? Please guide. Please note, this is just an example. Depending on the data supplied the other lines in the file may contain some different words. Not sure if this matters, but I'm on Windows 10 with Strawberry Perl version 5.32.

Enlightened Brothers, I am having bit (!) trouble with telling DBIx::Class to match records where a specific column of integer type (actually BIGINT) has specific bit(s) set. The equivalent SQL is something like: select * from TABLE where (roles & 4) = 1 (Edit: the RHS of this where expression, i.e.: =1 was written rather hastily and it does not make sense for checking if the 2nd LSbit is set. roles=4 is much clearer and probably faster. Instead, (roles&4) = 4 , (roles&4) > 0 . (roles&4)=0 make more sense in my particular scenario) Since we are on the topic, I have not yet understood how to use the column-name in a search with DBIx::Class. For example how to DBIx::Class this: select * from TABLE where (roles & 4) = (roles & 2) ? I can also settle for someone telling me how to pass a custom WHERE-in-SQL using DBIx::Class::ResultSet:: search() , if possible. Oh! MySQL latest bw, bliako

Hi. I am studying regular expressions and wanted to write a script that searches a DNA string for the longest substrings that consist of repeating letters. For example: CCCCC or GGG or AAAA etc. I managed to do that, but i am not very happy with the end resuslt. I was hoping to get most of the work done with a regex, in that regard i have failed. Furthermore there are statements in the while loop that look doubtful, and the idea of using an array to store the substring along with its length might not be good. Any advice is welcome. Thank you. use strict; use warnings; my $string = "AAATTTAGTTCTTAAGGCTGACATCGGTTTACGTCAGCGTTACCCCCCAAGTTATT + GGGGACTTT"; my @substrings; while($string =~ /([ACTG])(\1+)/g){ my $comb = $1.$2; my $len = length($1) + length($2); push @substrings, [$comb,$len]; } my @sorted = sort {$b->[1] <=> $a->[1]} @substrings; foreach my $substring (@sorted){ foreach my $element (@$substring){ print "$element "; } print "

"; } [download]

Hi

How I can visualize a Perl program? For example I like to see all the perl variables and the line-numbers or subroutines where it is used. I understand that variables can be created on the fly with eval and $$ and even another name-space but let's ignore for them for now, as my current code base doesn't have that. Thanks.

I'm trying to do some Excel hacking and Excel::Table looks like the perfect module. but I can't get it to open a spreadsheet. my 'open' subroutine is simple: sub getWB { my ($dir, $excel) = $_[0] =~ /(.*)\/(.*)/ ; my $xs = Excel::Table->new(dir => $dir) ; $xs->open($excel) ; return $xs ; } [download] and when I try to open the spreadsheet with 'getWB("D:myexcel.xlsx")' I get Log4perl: Seems like no initialization happened. Forgot to call init() + ? no such path [D:\myexcel.xslx] at D:\Perl\spreadsheet.pl line 55. [download] Any advice at what I'm missing/getting wrong? Thanks

Hi, I have a simple 30 line perl script which basically just compares a bunch of numbers in a list. I'm a little rusty with perl as I haven't used it in a few years now, but no matter what I try, I can't get past the syntax error which is complaining about the line where I compare two numbers out of my array. I've given up and just can't see what is wrong. Is there a monk out there who could guide me please? I hate to post up on such a simple thing, but I'm totally stuck. Running the script complains of a syntax error: $ ./perlAnalyzeDailyStats.pl syntax error at ./perlAnalyzeDailyStats.pl line 29, near "@array(" Execution of ./perlAnalyzeDailyStats.pl aborted due to compilation err + ors. [download] 1 #!/usr/bin/perl 2 3 @files = `ls -1trh s3_dailystats.*`; 4 foreach (@files) 5 { 6 $filename = $_; 7 #print $filename; 8 open(FH,"<$filename"); 9 while(<FH>) 10 { 11 $line = $_; 12 if($line =~ /([0-9]{17,18})/) 13 { 14 print "$1

"; 15 push(@array,$1); 16 last; 17 } 18 } 19 } 20 $arrayCount = @array; 21 print "array count is $arrayCount"; 22 foreach(@array) 23 { 24 if ($counter eq $arrayCount) 25 { 26 last; 27 } 28 $counter++; 29 print "difference is " . @array($counter) - @array($counte + r - 1); 30 } 31 [download]

Hi everyone, I have the following script: my ($query) = our $connect->prepare("SELECT number, name FROM TableNam + e"); $query->execute(); my $data = $query->fetchall_arrayref(); $query->finish; foreach $data ( @$data) { my %row_data; # get a fresh hash for the row data my ($variable1, $variable2) = @$data; $row_data{number} = "$variable1"; $row_data{name} = "$variable2"; } push(@loop_data, \%row_data); } [download] Not the most efficient way of doing this but achieves what I want. I would like to be able to do a similar thing, but for an unknown number of columns in the SQL Table. So: my ($query) = our $connect->prepare("SELECT * FROM TableName"); $query->execute(); my $data = $query->fetchall_arrayref(); $query->finish; foreach $data ( @$data) { my %row_data; # get a fresh hash for the row data foreach my $data_item (@$data) { $row_data{NAMEOFSQLCOLUMN} = "$data_item"; # How do I get the name + of the SQL column that the data_item belong to? } } push(@loop_data, \%row_data); } [download] Any help would be very much appreciated!

I feel like this is a really stupid question, but I'm sort of at my wit's end and really hoping someone can point out what it is that I'm missing. I have a subroutine and have some variables that are not in scope as I expected and can't figure out why, any wisdom on what I'm missing would be greatly appreciated sub make_ap_stanza () { my $policy = $_[0]; my $vsname; my $vsowner; my $owner; my $group; my $regexp; my $volumeset; my $volumeset_owner; print "Policy:$policy

"; my @attrs = @{ $Policy_HoA{$policy} }; foreach(@attrs){ if(($vsname) = ($_ =~ /vs_name =\s+(.*)/)){ print "VSNAME:$vsname

"; } elsif(($vsowner) = ($_ =~ /vs_owner =\s+(.*)/)){ print "VSOWNER:$vsowner

"; } elsif(($filter) = ($_ =~ /filter_name =\s+(.*)/)){ #print "FILTER:$filter

"; my @attrs = @{ $RegexHoA{$filter} }; foreach(@attrs){ if(($regexp) = ($_ =~ /REGEXP =\s+(.*)/)){ print "REGEXP:$regexp

"; } elsif(($owner) = ($_ =~ /OWNER =\s+(.*)/)){ print "OWNER:$owner

"; } elsif(($group) = ($_ =~ /GROUP =\s+(.*)/)){ print "GROUP:$group

"; } } } } print "foo $vsowner

"; } [download] So I declared a bunch of variables in the beginning of the code block, the nested routines populate them, but outside of the foreach loop, the variables are uninitialized??? [test1] change_timeout = 10 comment = foobar logmask = 0xffffff parent = thanos policy = perms policy = foobar queue_depth = 32 supports_hsm = yes path = /test1 Policy:perms REGEXP:image.([0-9]+).([0-9]+) OWNER:ANY GROUP:ANY VSNAME:dyn{2}{1} VSOWNER:root Use of uninitialized value $vsowner in concatenation (.) or string at + ./getconf line 395. foo [download] It looks like no matter how long I stare at this, I just can't understand why my variables are not properly declared

The test I needed help with in Can Test::MockObject mock a file? is finished. I would like to know how I did writing it. I got some advice on looping I did not quite get a handle on, but this is a short test for a little module. Here are links to the Fancy::Open module and Fancy::Open pod (unfinished). Read more... (4 kB) My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang. No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day! Lady Aleena