perl

Perl regex: how to know number of matches

Hi, I'm looping through a series of regexes and matching it against lines in a file, like this: for my $regex (@{$regexs_ref}) { LINE: for (@rawfile) { /@$regex/ && do { # do something here next LINE; }; } } Is there a way for me to know how many matches I've got (so I can process it acc...

String manipulation vs Regexps

We are often told that Regexps are slow and should be avoided whenever possible. However, taking into account the overhead of doing some string manipulation oneself (not talking about algorithm mistakes - this is a different matter), especially in PHP or Perl (maybe Java) what is the limit, in which case can we consider string manipulat...

Is the behavior of % with negative operands defined in Perl 5?

Until recently (i.e. C99), the behavior of the modulo operator was implementation defined in C. Since Perl 5 is written in C, is it reliant on the behavior of the C compiler used to build it? ...

what is regular expression for matching single line with multi match ?

$str = "Data = [ {"name": "test","Address": "UK" "currency": "£" },{"name": "test2","Address": "US" "currency": "$" },{"name": "test","Address": "eur" "currency": "E" } I want to display all address its not multi line string . It all a single string Please help on this Thanks , TREE J ...

Matching lines by number of words with regex

I am still new to regex and I've run into a bit of a problem. I am building a parsing script and I need to to be able to pull out lines with a certain length out of a file. How would I write a regex to match lines that have a certain number of words? Eg I want to match all lines in a file that have 3 words. Could I extend that to find...

Date formats for weeks

I'm having a difficult time, based on the documentation, of figuring out how to equate week numbers between Perl and MySQL. How would I calculate the same exact week number in both Perl and MySQL based on a unix timestamp in the same time zone? SELECT DATE_FORMAT(from_unixtime(datefield), '%Y%U') FROM table; and print strftime('%Y%U...

Perl best practices: file parser using regexes and database storage

Hi, I'm writing a log file parser in Perl, using regexes that I've stored in a database. My workflow is basically like this: Looping over the file and searching for patterns matching my regexes and then extract them Do something with these matches Store them accordingly in a database Last time I did this I explicitly wrote each rege...

Perl Importing Variables From Calling Module

I have a Perl module (Module.pm) that initializes a number of variables, some of which I'd like to import ($VAR2, $VAR3) into additional submodules that it might load during execution. The way I'm currently setting up Module.pm is as follows: package Module; use warnings; use strict; use vars qw($SUBMODULES $VAR1 $VAR2 $VAR3); requi...

How to make a hash of objects in perl

I would like to be able to store objects in a hash structure so I can work with the name of the object as a variable. Could someone help me make a sub new{ ... } routine that creates a new object as member of a hash? I am not exactly sure how to go about doing this or how to refer to and/or use the object when it is stored like this. I ...

How do I search for words that begins and ends with other words from the same array?

I have a long list of words in an array. Some short, some long. I'd like to filter out those words which starts with a word from the array (length of this "prefix" word can be set to, say, 3 characters) and which at the same time ends with a word from it. Let's say the first word is 'carport'. Now, if 'car' and 'port' exist in the array...

Removing punctuation from string in Perl

How do I remove all punctuation except for spaces from a string in Perl? ...

Extracting text in between strings in Perl

In Perl, how do I extract the text in a string if I knew the pre and post parts of the text? Example: Input: www.google.com/search?size=1&make=BMW&model=2000 I would like to extract the word 'BMW' which is always in between "&make=" and the next "&" ...

Why are multiple records selected from a database not displayed in my HTML?

After fetching the records from an SQL server database into a HTML form with select for update via a CGI program, the multiple selected items are not being displayed as selected. I am using Perl. use CGI; use CGI qw/:standard/; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); my $q = new CGI; my $query = new CGI; my @list =ne...

In Perl how do you create and use an array of hashes?

How to do a Perl program that contains an array and that array points a hash? It is like this pictorially, (M1) (M2) ...it goes on |--k1=>v1 |--K1=>v1 |--k2=>v2 |--k2=>v2 I should access that array M1, then the hash it contains inside. (and so on)... ...

How does Perl interact with the scripts it is running?

I have a Perl script that runs a different utility (called Radmind, for those interested) that has the capability to edit the filesystem. The Perl script monitors output from this process, so it would be running throughout this whole situation. What would happen if the utility being run by the script tried to edit the script file itself...

How do I enable a disabled context menu item when selection happens in a Perl Tk gui?

For example in the following script: use Tk; my $mw = new MainWindow; my $t = $mw->Scrolled("Text")->pack; my $popup = $mw->Menu( -menuitems => [ [ Button => 'Copy Selected', -state => "disabled", -command => sub {$t->clipboardColumnCopy} ], ] ); $t->menu($popup); MainL...

how to replace particular field in file

In a file : "name": "test","Address": "UK" "currency": "£" no:121212 , "name": "test1","Address": "UK" "currency": "£" no:12123212 , "name": "test2","Address": "UK" "currency": "£" no:121223212 , "name": "test3","Address": "UK" "currency": "£" no:121223212 , "name": "test4","Address": "UK" "currency": "£" no:121223212 , I want replac...

Only display one paragraph of text

You can set what the Facebook Share preview says. I would like it to be the first paragraph of my movable type entry. The people who make entries sometimes use <p> tags or they use the rich editor which puts in two <br /><br /> tags to separate paragraphs. Is there a way I can have movable type detect when the first paragraph e...

How do I replace the middle of a string?

$a = "<no> 3232 </no> " $a =~ s/<no>(.*)</no>/000/gi ; I am expecting that $a becomes "<no> 000 </no> ", but it is not working. ...

Extracting Ngrams as Words From Strings in Perl

Is there a module or Perl code that extract n-grams of words from a string besides Text::Ngrams ...