You Rule Avi
You Rule Avi

Reputation: 318

Perl Match Substring in a string ignore whitespace

I have a string

 $str = "xxxxxx Code File(s) Name:Some_thing.c CodeFile(s) Version:27 Design Document:some_other_design.doc Module Design Document Version:43 somexxxxxxxxxx Compiler Version:9.5 Doc Type:Word xxxxxx";

where xxxxx represents any character. But i am only interested in extracting the values of each attribute.

ie I want to save

$fileName = Some_thing.c;
$fileVersion = 27;
$designDocName = some_other_design.doc;
$designDocVersion = 43;
$compilerVersion = 9.5;

Right now I feel like I have a messed up solution. Just wondering if there is a cleaner way to do this. This might also fail if i have multiple files with multiple versions.

First i remove all whitespaces, and next i split the string into 2 till i get all the values

$str =~ s/\s*//g;

($temp,$temp2) = split(/CodeFile\(s\)Name:/,$str,2);
($fileName,$temp) = split(/CodeFile\(s\)Version:/,$temp2,2);
($fileVersion,$temp2) = split(/DesignDocument:/,$temp,2);
($designDocName,$temp) = split(/DesignDocumentVersion:/,$temp2,2);
($designDocVersion,$temp2) = split(/some/,$temp,2);
($testedCompilerVersion,$temp) = split(/CompilerVersion:/,$temp2,2);
($testedCompilerVersion,$temp2) = split(/DocType:/,$temp,2);

Please lead me to a link or an efficient solution. Thanks in advance.

PS: Please also check the comment below the question.

Upvotes: 0

Views: 316

Answers (3)

my ($fileName, $fileVersion, $designDocName, $designDocVersion, $compilerVersion) =
     $str =~ /Code File\(s\) Name:(.*) CodeFile\(s\) Version:(.*) Design Document:(.*) Module Design Document Version:(.*) somexxxxxxxxxx Compiler Version:(.*) Doc Type:(.*) xxxxxx/;

Upvotes: 0

Kenosis
Kenosis

Reputation: 6204

Perhaps the following will be helpful:

use strict;
use warnings;
use Data::Dumper;

my $str = "xxxxxx Code File(s) Name:Some_thing.c CodeFile(s) Version:27 Design Document:some_other_design.doc Module Design Document Version:43 somexxxxxxxxxx Compiler Version:9.5 Doc Type:Word xxxxxx";
my @labels = qw/fileName fileVersion designDocName designDocVersion compilerVersion docType/;
my ($i, %items) = 0;

$items{$labels[$i++]} = $1 while $str =~ /.+?:(\S+)\s+?/g;
print Dumper \%items

Output:

$VAR1 = {
          'designDocName' => 'some_other_design.doc',
          'fileName' => 'Some_thing.c',
          'docType' => 'Word',
          'designDocVersion' => '43',
          'fileVersion' => '27',
          'compilerVersion' => '9.5'
        };

Upvotes: 2

edi_allen
edi_allen

Reputation: 1872

Although I would go with @Kenosis solution I still wanted to show you who your script could be simplified.

#!/usr/bin/perl
use v5.14;
use warnings;

my $str = "xxxxxx Code File(s) Name:Some_thing.c CodeFile(s) Version:27 Design Document:some_other_design.doc Module Design Document Version:43 somexxxxxxxxxx Compiler Version:9.5 Doc Type:Word xxxxxx";

my ($fileName,
    $fileVersion, 
    $designDocName, 
    $designDocVersion, 
    $compilerVersion) = $str =~ /:(\S+)/g;

say "$fileName, $fileVersion, $designDocName, $designDocVersion, $compilerVersion";
#Some_thing.c, 27, some_other_design.doc, 43, 9.5

Upvotes: 0

Related Questions