Operating System - HP-UX
1831190 Members
2638 Online
110021 Solutions
New Discussion

Re: Need Help In Writing Perl Code

 
SOLVED
Go to solution
Virat
Occasional Contributor

Need Help In Writing Perl Code

Hi All,
Kindly Help Me in Getting Perl Code for following problem :

There is an XML file from which I need to get all the elements and their corresponding values. Following string has one such example or xml file. Can anyone help me getting this done?

$string="NG171TDCUAD\\NG171TDCUAD\D: Data23/11/2005 22:58:0313,567,900,09333minutesand52seconds382 MB/min";


Or at least if I can get value for element StartTime in any variable say $time? So $time should have value as 23/11/2005 22:58:03.
3 REPLIES 3
Hein van den Heuvel
Honored Contributor
Solution

Re: Need Help In Writing Perl Code

Hmmm, welcome to the ITRC HPUX Forum.

But pray tell, what does this question have to do with HPUX?

And, have you ever heard about Google?

You should try it some day. It's fun!
Suggested query: perl xml [I feel lucky]

This will likely bring you over to:

http://perl-xml.sourceforge.net/faq/

Read it.

Now if you wanted to just hack it and wack, and support it for the rest of your live, the you could roll your own. Something like:

use strict;
use warnings;
my ($key, %data);
my $i = 0;
my $string='NG171TDCUAD\\NG171T'.
'DCUAD\D: Data23/11/2005 22:58:03'zeMedia>13,567,900,09333minutesand52second'.
's
382 MB/min';

$_ = $string;
while (/<(\w+)>(.*?)<\/\1>/) {
$data{$1} = $2;
$_ = $`.$'
}

foreach $key (sort keys %data) {
$i++;
print "$i: $key --> $data{$key}\n";
}

I've removed the out tag as too confusing for simple code.
And I split the data line explicitlty.
And I used single quotes to avoid interpretation of the he backslashes in the text.

Good luck! (You'll need some.)
Regards,
Hein.
H.Merijn Brand (procura
Honored Contributor

Re: Need Help In Writing Perl Code

While it is tempting to parse HTML and XML with regular expressions, please try to avoid it, as you will hit every possible problem you thought could never happen somewhere along the line when the project evolves.

--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

use XML::Simple;

my $xml = <<'EOX';

NG171TDCUAD
\\NG171TDCUAD\D: Data
23/11/2005 22:58:03
13,567,900,093
33minutesand52seconds
382 MB/min

EOX

my $p = XML::Simple->new;
my $r = $p->XMLin ($xml);

print "$_:\t$r->{$_}\n" for grep m/time/i => keys %$r;
-->8---

# perl test.pl
ElapsedTime: 33minutesand52seconds
StartTime: 23/11/2005 22:58:03

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Need Help In Writing Perl Code

While it is tempting to parse HTML and XML with regular expressions, please try to avoid it, as you will hit every possible problem you thought could never happen somewhere along the line when the project evolves.

--8<--- test.pl
#!/pro/bin/perl

use strict;
use warnings;

use XML::Simple;

my $xml = <<'EOX';

NG171TDCUAD
\\NG171TDCUAD\D: Data
23/11/2005 22:58:03
13,567,900,093
33minutesand52seconds
382 MB/min

EOX

my $p = XML::Simple->new;
my $r = $p->XMLin ($xml);

print "$_:\t$r->{$_}\n" for grep m/time/i => keys %$r;
-->8---

# perl test.pl
ElapsedTime: 33minutesand52seconds
StartTime: 23/11/2005 22:58:03

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn