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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: file is replaced
by gmargo (Hermit) on Nov 30, 2009 at 06:09 UTC
      Can't use an undefined value as a symbol reference at new.pl line 66, +<DATA> line 4. #!/usr/bin/perl use strict; use warnings; my $tag; my $output; my $fh; my $flag =''; my $output_text; my $i=0; my $file=''; while (<DATA>) { s/.*?\{IT\}/{IT}/g; s/{IT}R/{IT}\nR/g; chomp; s/[\cA-\cZ]//g; s/\^[A-Z]//g; if(/^{(.*)}$/) # match {METATAG} line { $fh = xml_output($output, $tag, $fh,$i); $output = ""; $tag = $1; print "The tag is---->$tag\n\n"; } else { # not a {TAG} line next unless($tag); next if(/^\s*$/); $output .= ($output) ? " $_" : "<$tag>$_"; } # if($output =~ m/<IT>(.*)/){ $i++; } } # End of While Loop $fh = xml_output($output, $tag, $fh,$i); if($fh) { print $fh "</ROOT>\n"; close($fh); } exit(0); # Subroutine to open the file with the filename as {TAG} sub xml_output { my ($output, $tag, $fh,$i) = @_; if($output) { #print "The file name inside $file\n"; if($output =~ m/<IT>/) { if($fh) { print $fh "</ROOT>\n"; close($fh); } open($fh, '>', "$i.xml") or die "$1.xml: $!"; print $fh "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<RO +OT>\n"; } $output =~ s/\s*(?=(<.+>|<.+\/>|<\/.+>|<\/.+><.+>))//g; print $fh "$output</$tag>\n"; } return($fh); } # End of sub sroutine __DATA__ 020200001117VUTVS01 00000745B3^V^V^A2^C^D^V^V^A 0000 0001 090104 N S00 +00000000 00001889^B{IT}R {DATE} 090104 {TDATE} Sunday, January 4, 2009 {SECTION} H5 {HEADLINE} LEGISLATORS VISIT SENIOR APARTMENT COMPLEX {BODY} 2005 with state and federal funding. < {IT} R {DATE} 050102 {TDATE} Sunday, January 02, 2005 {EDITION} 6 {TAG} 0412270403 {BODY} Certified Financial Planner for DiStefano Finacial Group in Westfield +, MA.
      Can't use an undefined value as a symbol reference at new.pl line 66, <DATA> line 4.
      I am getting an error.
      I have added the code to delete strings before {IT}.
      But still it gives out error message.

        You only open $fh somewhere down below in your xml_output routine, but use it all over the place. Most likely you're trying to write to $fh before having opened it. Most likely, this when your program reaches line 66.

Re: file is replaced
by keszler (Priest) on Nov 30, 2009 at 10:30 UTC
    holli gave you a complete, working, OO-based program with reusable modules at Re: Split file based on tag in response to your Split file based on tag. You previously had answers at file split, print line, and delete line. You then asked again at change the filename.

    If for some reason you cannot use holli's excellent solution to your problem, you should be able to extract the techniques you need from that code.

    To start with, read the open documentation, then look for it in your script. Check the variables used by open in your script and trace them back to where they were set. Then look at holli's Re: Split file based on tag and do the same. Change your script based on what you just learned. If it then does what you want - great! If not, come back here, post what you did, what you thought would happen, and what did happen.

    There are many Monks here happy and willing to help you, but very few willing to do the work for you. Show some effort, explain your thinking as to what should happen, and someone will gladly guide you.

      020200001117VUTVS01 00000745B3^V^V^A2^C^D^V^V^A 0000 0001 090104 N S00 +00000000 00001889^B{IT}R
      If I use the {IT} like this, I code doesn't works.
      while (<DATA>) { s/.*?\{IT\}(.*)/{IT}\n$1/g; s/^\s*//g;
      I tried substituting the {IT} as above.
      It's giving the error as Can't use an undefined value as a symbol reference at new.pl line 64, <DATA> line 4.
      When I try to print,
      print "The tag is---->$tag\n\n";
      IT is not taken into consideration as tag value.
      but gives out the error message as Can't use an undefined value as a symbol reference at new.pl line 64, <DATA> line 4.
Re: file is replaced
by colwellj (Monk) on Nov 30, 2009 at 05:39 UTC
    Have a look at $i in sub xml_output.
    It's always 0 when you create the file.
    Try passing $i in from the main program and incrementing it there.
    A reply falls below the community's threshold of quality. You may see it by logging in.