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

Perl Monks, Newbie to perl, running this script below is not outputing any data, plforecast.txt is 0 bytes, any help will be great
$,="\t"; open(IN,"<hcspln_plforecast.txt"); open(OUT,">plforecast.txt"); $_=<IN>; $_=<IN>; while(<IN>) { y/"//d; if(/^No_Company/) {next;} s/#Mi/0.00/g; split(/,/); if($_[1] =~ /CC_/) { if($_[5] =~ /^PL_/) { $_[0] =~ s/COCD_//; $_[5] =~ s/^PL_//; print OUT $_[0],$_[1],$_[5]; splice(@_,0,8); print OUT "",@_; } } }
  • Comment on Perl Monks, Newbie to perl, running this script below is not outputing any data, plforecast.txt is 0 bytes, any help will be great
  • Download Code

Replies are listed 'Best First'.
Re: Perl Monks, Newbie to perl, running this script below is not outputing any data, plforecast.txt is 0 bytes, any help will be great
by toolic (Bishop) on Jan 07, 2015 at 16:28 UTC
Re: Perl Monks, Newbie to perl, running this script below is not outputing any data, plforecast.txt is 0 bytes, any help will be great
by Eily (Monsignor) on Jan 07, 2015 at 16:49 UTC

    This code won't work on later versions of perl. split used to output the splitted data to @_ in v5.10 and earlier when called in scalar or void context. Starting with v5.12 (or maybe some subversion of v5.10, I didn't check that far) you'll have to write @_ = split /,/ explicitly, or even better, use another variable than @_ because of its special meaning in perl.

      Boss your help is appreciated, the change you mentioned picked up
Re: Perl Monks, Newbie to perl, running this script below is not outputing any data, plforecast.txt is 0 bytes, any help will be great
by blindluke (Hermit) on Jan 07, 2015 at 16:28 UTC

    Hello, perlnobie, and welcome to the Monastery.

    Please, use  <code> </code> tags to make your code readable. Try telling us what the script is supposed to do. You seem to be troubled by the fact that it does not output any data. What data should it produce?

    The code you posted starts by opening a file for input. What is this input? The code you posted manipulates the input data. If it is empty, or does not fit the expected patterns, then no wonder that the output file is empty.

    Help us by providing some further information.

    - Luke

      Thanks Luke, The script is looking at a source file and then outputing to produce new formatted file. when exportign it is creating the file with the name but not the content in it.
        The script is looking at a source file and then outputing to produce new formatted file. When exporting it is creating the file with the name but not the content in it.

        The first sentence is obvious, and the second sentence repeats the fact that you stated in the title of your original post. You still are not telling us what the script should do. Should it copy all lines from input to output? Only some of them? Should it manipulate the lines in some way?

        The code itself would benefit greatly from adding use warnings;, and, as toolic already suggested to you, running your script with warnings enabled would tell you what is wrong with it.

        In addition, the script you posted relies very heavily on the implicit variables,  $_ and  @_. Both those variables are very useful, but require some understanding. Take a look at a good explanation of their usage in the Modern Perl book.

        Take a good look at the split(/,/); line. It should split the line from the input file into separate fields. Are you sure you understand how it works here? Have you tried looking at the documentation for split? Usually, it's used like this:

        my @values = split(',', $data);

        Think about split usage in your script. You do not specify the $data variable. What is being split? You do not specify the @values variable. Where do the split values go? And probably the most important question: are you sure that is where the split values go?

        - Luke

Re: Perl Monks, Newbie to perl, running this script below is not outputing any data, plforecast.txt is 0 bytes, any help will be great
by GotToBTru (Prior) on Jan 07, 2015 at 16:37 UTC

    I would run the script using the debugger; you can see if your regexes are working as expected. See here for how to get started with the debugger.

    1 Peter 4:10
Re: Perl Monks, Newbie to perl, running this script below is not outputing any data, plforecast.txt is 0 bytes, any help will be great
by vinoth.ree (Monsignor) on Jan 07, 2015 at 17:40 UTC
    Hi perlnobie

    When I ran your program I got error as "Useless use of split in void context at..." So I changed the code little bit, please have a look at, below, it works fine now.

    #!/usr/bin/perl use strict; use warnings; $,="\t"; open(IN,"<hcspln_plforecast.txt"); open(OUT,">plforecast.txt"); $_=<IN>; $_=<IN>; while(<IN>) { y/"//d; if(/^No_Company/) {next;} s/#Mi/0.00/g; my @Array = split(/,/); if($Array[1] =~ /CC_/) { if($Array[5] =~ /^PL_/) { $Array[0] =~ s/COCD_//; $Array[5] =~ s/^PL_//; print OUT $Array[0],$Array[1],$Array[5]; splice(@_,0,8); print OUT "",@_; } } }

    Here, I used @Array variable to hold the splitted values.

    As you have not given as sample input file content from hcspln_plforecast.txt assumed as follow and test the script.

    hcspln_plforecast.txt
    line:1 line:2 COCD_1234,CC_line3,c,d,e,PL_line3 COCD_2344,CC_line4,h,h,i,PL_line4

    All is well
      Thanks Much, Appreciate your concern and inputs.