1

I have strings like

     CASE: 8
     Location: smth
     Destination: 183, 3921,  2.293e-2, 729, 9
     END_CASE

I need to put number of CASE (8) and Destination parameters to variables... How to do that?

4
  • my %data = map split(/:\s*/, $_, 2), split(/\n/, $string); Commented Feb 10, 2014 at 12:20
  • 1
    Did you try anything yourself? Commented Feb 10, 2014 at 13:13
  • I've put my input file to @mass and using foreach loop, tryed to check if it matchs with my regexp, but it wont work because it checks line by line... Commented Feb 10, 2014 at 13:20
  • 1
    @Rocker - If you've made an attempt you must always include that in your question. It will make it easier for people to understand what you're trying to do, and more willing to help out. Commented Feb 10, 2014 at 15:58

1 Answer 1

1

Here is with regexp:

my $str = "CASE: 8
Location: smth
Destination: 183, 3921,  2.293e-2, 729, 9
END_CASE
        ";
my ($case,$dest) = $str= m!\A\s*CASE:\s*(\d+).+?Destination:\s*(.+?)\n!gis;
print "case: $case, dest: $dest\n";

EDIT:

If you would like to match multiline regexp, and your file is small you could slurp it.

If it is bigger then you could process it in chunks (blocks).

slurp:

local $/=undef;
open(my $fh,'<',...) or die $!;
my $str = <$fh>;
while ($str= m!\A\s*CASE:\s*(\d+).+?Destination:\s*(.+?)\n!is){
  print "case: $1, dest: $2\n";
}

process in chunks:

my $str;
while( my $line = <$fh>){
  if ($line !~ m!END_CASE!){
    $str .= $line;
  } else {
    $str .= $line;
    ### process $str
    my ($case,$dest) = $str= m!\A\s*CASE:\s*(\d+).+?Destination:\s*(.+?)\n!gis;
    print "case: $case, dest: $dest\n";
    ### reset chunk
    $str = '';      
  }
}

Regards,

Sign up to request clarification or add additional context in comments.

1 Comment

Id like something like while(<$infile>){ if (regexp matchs the text){put collectet arguments into variables)}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.