Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] REGULAR EXPRESSION IN SPLIT FUNCTION - PLEASE HELP ME.

Expand Messages
  • Charles K. Clarkson
    From: ... into ... remaining ... While I m sure you could get it to work with split, m// works fine: @res = $data =~
    Message 1 of 1 , Mar 1 6:55 AM
    • 0 Attachment
      From: <ponnambalam.ma@...>

      : DEAR FRIENDS,
      : PLEASE HELP ME REGARDING THE REGULAR
      : EXPRESSION IN SPLIT FUNCTION
      :
      : data is : CDM210909 or FDM210909 ( ie start with 3characters followed
      : any no of numeric numbers)
      : probelm is : i need to split this word into CDM and 210909 ( split
      into
      : characters & numbers).
      :
      : --- ie, i wish to split this word into char (first 3 characters &
      remaining
      : )
      :
      : @res=split( / .{3}/ , $data );

      While I'm sure you could get it to work with split, m//
      works fine:
      @res = $data =~ /(\w{3})(\d*)/;
      or if the data runs together:
      my $data = 'CDM210909FDM210909';
      my @res = $data =~ /(\w{3})(\d*)/g;

      If you have a list of data (@data):
      @res = map /(\w{3})(\d*)/, @data;


      HTH,
      Charles K. Clarkson

      Notes:
      When you match with a expression using m// you normally
      refer to the patterns match in () (aka 'back referenced') by
      numbers. In /((\w{3})(\d*)/ you can refer to the first back
      reference with $1 and the second back reference with $2.
      When called in 'list context', like we did here,
      /(\w{3})(\d*)/ will act as array would. Actually it returns a
      list (because it was called by a list). So if
      $data = 'CDM210909' we get a list back like:
      ('CDM', '210909').

      It's worth metioning that calling a pattern like this with a
      hash will also return a list.

      my @data = qw/ CDM210909FDM210909
      LDM210909KDM210909/;
      my %res = map /(\w{3})(\d*)/g, @data;
      %res will end up like this:
      {
      'KDM' => '210909',
      'CDM' => '210909',
      'FDM' => '210909',
      'LDM' => '210909'
      };
    Your message has been successfully submitted and would be delivered to recipients shortly.