in reply to Re: Parsing an file that has xml like syntax
in thread Parsing an file that has xml like syntax

I think this is the way to do it.
use strict; use Data::Dumper; my $config; while (<DATA>) { chomp; my ($key, $variable)= ( $_ =~ /<(\w+?)>([^<]*)/); if ($key =~ m/JOBID/){ push (@{$config->{JOBID}},$variable); }else{ $config->{$key} = $variable; } } close FILE; print Dumper $config; __DATA__ <PROJECT_ID>12345</PROJECT_ID> <JOBID>101</JOBID> <JOBID>102</JOBID> <JOBID>103</JOBID> <TYPE1>add</TYPE1> <FILE1>/tmp/file_data_gros</FILE1> <JOBID>102</JOBID> <TYPE2>delete</TYPE2> <FILE2>/tmp/file_myvalues</FILE2>
Output
$VAR1 = { 'JOBID' => [ '101', '102', '103', '102' ], 'FILE2' => '/tmp/file_myvalues', 'TYPE2' => 'delete', 'FILE1' => '/tmp/file_data_gros', 'TYPE1' => 'add', 'PROJECT_ID' => '12345' };

Replies are listed 'Best First'.
Re^3: Parsing an file that has xml like syntax
by Anonymous Monk on Apr 02, 2014 at 19:56 UTC

    Hi crusty_collins I think I understand what you are doing here. Are you creating an array of jobid values and then associating each jobid value with the corresponding value for TYPE and FILE? So for instance if this was seen in the file:

    <PROJECT_ID>12345</PROJECT_ID> <JOBID>101</JOBID> <TYPE1>add</JOBID> <FILE1>/tmp/file_data_gros</FILE1> <JOBID>104</JOBID> <TYPE2>delete</TYPE2> <FILE2>/tmp/file_myvalues</FILE2>

    I would need to parse this file like so: found value for first JOBID and also found values for the associated FILE1 and TYPE1 and will now execute my program with these values:

    `myprogram -action add -file /tmp/file_data_gros`

    Then loop would then carry on to see if there was another JOBID and if there is, which there is in this example, then my program would execute:

    `myprogram -action delete -file /tmp/file_myvalues

    then loop would carry on to see if there was another JOBID and if there was if would find the value to the next TYPE* and FILE* fields defined and then execute my program again

    I don't fully understand your code but how would I loop over this file to do the above and execute my program?