in reply to Parsing an file that has xml like syntax

Your split is going to return more than you expect. There will be 4 values returned by the split of your first line:

0 '' 1 project_id 2 12345 3 /project_id

Try:

($xmltag, $xmlvalue) = (split /\<|\>/, $string)[1,2];

The parentheses tell perl to treat the returned values from split as an array, and the subscripts pick out the elements you are specifically looking for.

I do the same thing in several of my programs using the following:

($tag,$value) = ($inline =~ /<(\/?\w+?)>([^<]*)/);

I'm using a regex to handle 3 cases I see in my input:

<tag>value</tag> or <tag> or </tag>

I can supply the details on how this works, but you might find it informative to try to figure out how it works yourself.

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

    That's brilliant, that works really well. Thanks a lot. I have been trying for ages to get the tag and value out correctly. Would you be able to help me with a loop that for each JOBID it would get the value of the corresponding TYPE* value and FILE* value and print this info on one line of output. So the first line of output would be:

    12345 101 add /tmp/file_data_gros

    Then the loop moves onto find the next JOBID and the values for the associated TYPE* and FILE. The next line printed would be:

    12345 102 delete /tmp/file_myvalues

    And will continue to do this until all JOBID's and associated TYPE*s and FILE*s have been processed in the file