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

Hi All, Here is the file I need to parse.

---------------------------------------

Docs Legal:Legal100-106;QA:QAServer5
Legal Legal:Legal15;QA:QAServer5
Funding QA:Alpha,Beta;PROD:PROD1-5
-------------------------------------

if i parse the first line I should get .......
Docs Legal legal100
Docs Legal Legal101
Docs Legal Legal106
Docs QA QAServer5
....... and so on.

Here the delimiters are ; and : with ; separating the environments and : seperating the machines to be deployed from the environemnt name.

I tried parsing this and I ended up coding like any language with lots of if loops and splits. Is there a easy way of doing the above.

Thanks,
Ganesh

  • Comment on Parsing a file - For differnet environments

Replies are listed 'Best First'.
Re: Parsing a file - For differnet environments
by kvale (Monsignor) on Mar 22, 2004 at 18:47 UTC
    Here is a way to parse the config file:
    while (<DATA>) { my ($prefix, $rest) = split; foreach my $env (split /;/, $rest) { my ($env_name, $machine) = split /:/, $env; print "$prefix $env_name $machine\n"; } } __DATA__ Docs Legal:Legal100-106;QA:QAServer5 Legal Legal:Legal15;QA:QAServer5 Funding QA:Alpha,Beta;PROD:PROD1-5
    which produces
    Docs Legal Legal100-106 Docs QA QAServer5 Legal Legal Legal15 Legal QA QAServer5 Funding QA Alpha,Beta Funding PROD PROD1-5
    Update: That 100, 101 and 106 look a little suspicious. If you meant to expand the number intervals as well, a little extra code is needed:
    while (<DATA>) { my ($prefix, $rest) = split; foreach my $env (split /;/, $rest) { my ($env_name, $machine) = split /:/, $env; if ($machine =~ /^(\D+)(\d+)-(\d+)$/) { my $root = $1; foreach $num ($2..$3) { print "$prefix $env_name $root$num\n"; } } else { print "$prefix $env_name $machine\n"; } } } __DATA__ Docs Legal:Legal100-106;QA:QAServer5 Legal Legal:Legal15;QA:QAServer5 Funding QA:Alpha,Beta;PROD:PROD1-5

    -Mark