http://qs321.pair.com?node_id=11123126

pritesh_ugrankar has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Even after multiple attempts, I am at a total loss of how the "m" and "s" works for regex. I have a file like this:

first: this:that here:there when:what how:where now:later second: this:that here:there when:what how:where now:later

Please note that in the example, "this:that", "here:there", etc are repeated, but that's not the case with the actual record I am working on. I am trying to write a script that will create a hash of hashes such that I get a hash with a key "first" and its value will be a key value pair such that the key will be "this" and value will be "that", key will "here" and value will be "there", and so on and so forth. But even before I reach there, I need to ensure that I write the correct regex. So, I tried to write a regex that will skip the line if it contains "first" or "second". Of course if the regex works, I can then capture the part and make that as a hashkey, but that's much later.

I've tried the following, but it does not work. And I am pretty sure it's because my utter failure to understand how "m" and "s" work.

use strict; use warnings; use Data::Dumper; my $file = "new_testfile.txt"; my $testhashref; open (my $fh, "<",$file) or die "Can't open open file $file:$!"; { local $/ = ""; while (my @records = <$fh>) { foreach my $line (@records) { next if $line =~ /^[a-z]+:$/m; print "$line"; } } }

I tried using "s" instead of "m", but when I run the script, it does not read anything.

pritesh@pavilion:~/perlscripts$ perl test.pl pritesh@pavilion:~/perlscripts$

If I remove the next if $line =~ /^[a-z]+:$/m;, I get the whole file like so:

pritesh@pavilion:~/perlscripts$ perl test.pl first: this:that here:there when:what how:where now:later second: this:that here:there when:what how:where now:later

So at least I know it's reading the records right. I will be thankful if you could help me with this one.