Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

how could i captured selected data of the input ?

by bh_perl (Monk)
on Nov 09, 2006 at 03:02 UTC ( [id://583034]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

This is my program :-

#!/usr/bin/perl use Getopt::Std; use Cwd; use vars qw /%opt/; my $options = 'h:i:l:v:'; my %fldinfo; my @entries = (); my $dir = getcwd; getopts("$options", \%opt) or usage(); sub usage { print "Usage: $0 -[h] -i <input_file>\n"; if ($opt{h}) { print " -h : help\n"; print " -i : xml input file \n"; print " -l : list of field will be changes\n"; print " -v : assigned the change values\n"; } } sub change_specified_field { my $fvalue = $opt{v}; my ($ff, $fv) = (split(/=/,$opt{v}))[0,1]; $ff = lc($ff); $fv =~ s/(\S{2})/$1 /g; $fv =~ s/\s$//g; open(TMP, "+> $dir/$opt{i}.tmp"); open(DATA, "$opt{i}"); while (my $data = <DATA>) { chomp($data); $data = lc($data); foreach (split(/CallDataRecord/,$data)) { if ($_ =~ /$ff/) { print TMP "$1>$fv<$3\n" if /(.*)>(.*)< +(.*)/; } else { printf TMP "$_\n"; } } } close(DATA); close(TMP); system("mv $dir/$opt{i}.tmp $dir/$opt{i}"); } main { if ($opt{h}) { usage(); } if ($opt{i} && $opt{v}) { change_specified_field(); } exit(); }

My program will be read the input data and split it by "calldatarecord". The problem is how could i captured only specified input data records of the input data.

As example, this is some example of the input data :-

------------------------------------------------- <calldatarecord> <timeforstartofcharge>08 17 09</timeforstartofcharge> <timeforstopofcharge>08 17 15</timeforstopofcharge> <chargeableduration>00 00 0A 0D</chargeableduration> </calldatarecord> <calldatarecord> <timeforstartofcharge>08 16 17</timeforstartofcharge> <timeforstopofcharge>08 17 16</timeforstopofcharge> <chargeableduration>00 00 0A 0D</chargeableduration> </calldatarecord> --------------------------------------------------------

Then, 1. how could and print the second record only ? 2. how could i print the "chargeableduration" values of the second record ?

could somebody help me ?

Thenk you,

Edited (davorg): removed code tags from non-code sections

Replies are listed 'Best First'.
Re: how could i captured selected data of the input ?
by davido (Cardinal) on Nov 09, 2006 at 03:12 UTC

    You could use XML::Twig to parse the data for you, dumping it into a datastructure. Then you've got easy access to individual elements. If you want to get a snapshot of the output that XML::Twig is giving you, just print the datastructure using Data::Dumper. XML::Simple would do the trick too, but I like the reliability of XML::Twig better. See the POD for information on its simplified interface.


    Dave

Re: how could i captured selected data of the input ?
by dimar (Curate) on Nov 09, 2006 at 14:06 UTC
    In addition to the modules suggested in previous posts, you probably also want to take a look at how you are handling the raw data (assuming it really is XML, and not just a deceptively similar analogue) after you have loaded it. It looks like you are loading a simple table structure, so all you need to do is treat your data like a table in your code. There are modules that will help you do that as well (see e.g., DBD-AnyData). Hence, to directly answer your questions here is a psuedo-code example:
    my $oTable = XML::Simple::XMLin($sFile); print $oTable->[1]; ## 1. print second record only print $oTable->[1]{chargeableduration}; ## 2. do that
    This is pseudo-code because it ignores the real code needed to fulfill these assumptions: 1) you easily loaded your XML into an AoH; 2) you know the code to print out the second record is actually a bit more involved if you want some useful output; 3) you know the difference between an AoH and an AoA. If these assumptions are beyond your familiarity, have a shot at supersearch and let the learning begin.

    =oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://583034]
Approved by davido
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-19 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found