Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Manipulating a file

by Siegfried13 (Initiate)
on Apr 30, 2014 at 12:42 UTC ( [id://1084498]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, Im currently working on perl script that will covert the content on a given pattern. I want to make my code dynamic so I used the sub routine function so that it can handle the multiple patterns to be converted. Since per data it has an inconsistent Heading 2 content. Ex. <Heading 2>General No. 1289 <Heading 2>SRL Nos. 1312 & 1294 it should have an output of GeneralNo.1289 and SRLNos1312 _1294 Does the use of my subroutines are correct? And Does the flagging of varibles are correct in my codes below? I'm new to perl and I'll really appreciate your help to correct my errors below. Thank You!

#!/usr/bin/perl use warnings; $input_data = @ARGV[0]; $output_data = @ARGV[1]; $h2_flag = 0; $h3_flag = 0; open(INFILE,"$input_data") || die "cannot open input file : $infile"; open(OUTFILE,">$output_data") || die "cannot open temporary output fil +e : $output_data"; $/=undef; # Process error data to rectify the error records in massaged data while (<INFILE>) { $data = $1; if ($data =~ m/(<RD,ID:[A-Z0-9]*:"Heading\s*2">.*)/) { $h2_flag++; }elsif ($data =~ m/(<RD,ID:[A-Z0-9]*:"Heading\s*3">.*)/) { $h3_flag++; } if ($h2_flag == 1) { if ($data =~ m/(<RD,ID:[A-Z0-9]*:"Heading\s*2">.*)/) { $head_data = $1; $head_data =~ s/<RD,ID:[A-Z0-9]*:/<RD:/gi; } } &heading; sub heading{ if ($h2_flag == 1) { if ($data = m/(<RD,ID:[A-Z0-9]*:"Heading\s*2">.*)/) { $head_data = $1; $Case_No = $head_data = s/(<.*>)(.*)/$2/; $Case_No = $head_data; $date = $head_data; $h2_flag = 0; }elsif ($h3_flag == 1) { print "PRINT"; $h3_flag = 0; } } print OUTFILE $_; # Output the contents of $_ t +o # output file } # Close the INFILE and OUTFILE. close(INFILE); close(OUTFILE);
Hi, This is the sample of the data: <DR:"Heading 2">T.G No. L-5786 December 29, 2010 <DR:"Heading 3">ASIAN vs. ET AL. Sample output on should be all special character will be remove. and the date will be converted as 12292010. Output: <Head>TGNL5786Y12292010 Description: TGNL5786 - Head Y - Consistent Indicator of date. Date - 12292010 December will be 12 Date is 29 and Year is 2010. the month word will always be converted into its numeric value. Januar +y - 01

Replies are listed 'Best First'.
Re: Manipulating a file
by rnewsham (Curate) on Apr 30, 2014 at 13:18 UTC

    There are some problems with the code you pasted. It will not compile as there is a missing } somewhere, as you did not indent the while block it is not clear where it should be but I guess before &heading. Which brings us to problem two you call &heading but there is no sub heading, you probably want to rename heading2 to heading. A couple of other minor points you probably want $ARGV[0] instead of @ARGV[0]. It is also more common to use heading() instead of &heading.

    As for what your code is doing and whether or not it is the best approach, I agree with GotToBTru. It would help if you provided an example of the input data and an example of the desired output.

Re: Manipulating a file
by NetWallah (Canon) on Apr 30, 2014 at 15:14 UTC
    Your code requires a "}" to be inserted at line 37, to compile, has several warnings, and the line 19:
    $data = $1;
    makes no sense.

    Please make an attempt at actually addressing the problem, post code that compiles, and ask specific questions about the parts that do not do what you expect.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: Manipulating a file
by GotToBTru (Prior) on Apr 30, 2014 at 12:55 UTC

    It would help to see a sample of the data file.

    1 Peter 4:10

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found