Rajeev wrote:
>
> Hi Rob,
>
> I have written a routine here .. not exactly whay u told.. before that I have a 
> problem with regex..
>
> I want to pass the regex on which the splitting (or record separation logic in my 
> code) will happen such that I can call the same
function with different record splitting criteria.
>
> Somehow this does not work ..
> I tried passing $seperator = '/(?m:^\s*\n$)/';
> and later tried to do a match with $serapator .. this fails..
>
> Please tell me what shall I do.
> **************************************************************
> my $seperator = '/(?m:^\s*\n$)/';
> my %rec = GET_RECORD([EMAIL PROTECTED], $separator);
>
> sub GET_RECORD {
>     my ($buffer, $separator) = @_;
>     my %record = ();
>
>     foreach $line (@{$buffer}) {
>         #if($line =~ $seperator) {  -------->>>> DOES NOT WORK
>         if($line =~ /(?m:^\s*\n$)/) { --------->>> This works
>             $i++;
>         } else {
>             push @{$record{$i}} , $line;
>         }
>     }
>     return %record;
> }
> ***************************************************************

I don't think it 'fails' does it? The regex you're passing is
valid as far as I can tell, but nonsensical. It will match
the following sequence:

- an optional open parenthesis
- a lowercase 'm'
- a colon
- the start of a new record
- zero or more whitespace characters
- a newline
- the end of a record

But if you say that this is what you want then that's fine.

The main reason it's not working is that you've written

  my ($buffer, $separator) = @_;
                   ^
    :
  if ($line =~ $seperator)
                   ^

Also, it will help if you write

  my $separator = qr/(?m:^\s*\n$)/;

so that the string is parsed as a regex at compile time.

If you're indexing 'record' with an integer then you should use
an array:

  my @record;
    :
  push @{$record[$i]}, $line;

Over all, it looks like you're working far too hard to achieve
a simple result. The code below will do what you seem to want.

Please post responses back to the list.

Cheers,

Rob


use strict;
use warnings;

my @record;
my $i = 0;

open IN, 'abc' or die $!;

while (<IN>) {
  chomp;
  if (/\S/) {
    push @{$record[$i]}, $_;
  }
  else {
    $i++ if @{$record[$i]};
  }
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to