Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: HELP TO WRITE THE PERL CODE

by Anonymous Monk
on Sep 01, 2015 at 03:06 UTC ( [id://1140595]=note: print w/replies, xml ) Need Help??


in reply to Re: HELP TO WRITE THE PERL CODE
in thread help to write perl code

perlintro, xpather.pl , Modern Perl, ...

#!/usr/bin/perl -- ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use XML::LibXML 1.70; ## for load_html/load_xml/location my( $xml ) = q{<reg><bit><init_val>0</init_val></bit><bit><init_val>0< +/init_val></bit><bit><init_val>1</init_val></bit><bit><init_val>0</in +it_val></bit><bit><init_val>1</init_val></bit><bit><init_val>1</init_ +val></bit><bit><init_val>0</init_val></bit><bit><init_val>0</init_val +></bit><bit><init_val>0</init_val></bit><bit><init_val>1</init_val></ +bit><bit><init_val>0</init_val></bit><bit><init_val>0</init_val></bit +><bit><init_val>1</init_val></bit><bit><init_val>1</init_val></bit><b +it><init_val>0</init_val></bit><bit><init_val>0</init_val></bit></reg +>}; RunWithIt( $xml ); exit( 0 ); sub RunWithIt { my $dom = XML::LibXML->new( qw/ recover 2 / )->load_xml( string => + shift ); for my $reg ( $dom->findnodes( q{ //reg } ) ) { my $bitstr = FunWithIt( $reg ); my $bytes = BitsToBytes( $bitstr ); print "$bitstr The answer is $bytes\n"; } } ## end sub RunWithIt sub BitsToBytes { return pack 'b*', shift; } ## end sub BitsToBytes sub FunWithIt { my( $reg ) = @_; my $str = ''; for my $bit ( $reg->findnodes( q{ //bit } ) ) { $str .= $bit->findvalue( './/init_val' ); } return $str; } ## end sub FunWithIt

Replies are listed 'Best First'.
Re^3: HELP TO WRITE THE PERL CODE
by achs (Initiate) on Oct 06, 2015 at 11:31 UTC
    Thanks very much for the reply. I see that you have given in a variable. My input xml file is huge which I cannot copy and paste in the script. Is there a way in which I could pass the input file into the code given above and then do the processing.

      Naturally there is. The basics are covered in perlintro which also contains links to yet more extensive documentation.

      Thanks very much for the reply. I see that you have given in a variable. My input xml file is huge which I cannot copy and paste in the script. Is there a way in which I could pass the input file into the code given above and then do the processing.

      If you want to change this self-contained program to read xml from a filename, you need to read XML::LibXML

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1140595]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found