Why am I getting weird output from Perl using SQL?

Here is my Perl code:

foreach my $line  (@tmp_field_validation)
{
        chomp $line;
        my ($cycle_code,$cycle_month,$cycle_year)= split /\s*\|\s*/, $line;
        $cycle_code=~ s/^\s*(.*)\s*$/$1/;
        $cycle_month=~ s/^\s*(.*)\s*$/$1/;
        $cycle_year=~ s/^\s*(.*)\s*$/$1/;
        print "$line\n";
        print "$cycle_code|$cycle_month|$cycle_year";
}

      

Here's the result:

         1          10       2009
1           10       2009||

      

What's wrong here? I expected pipes to be between variables. Why are pipes printed after all three variables?

EDIT: tmp_field_validation is the result of a sql query that has a select statement like:

select cycle_code,cycle_month,cycle_year from ....

      

so the output will come as 3 different columns when I run the query in TOAD. but the same request, when used in this script, how can it be possible that the output is considered the only field of cycle_code

+2


a source to share


2 answers


You should add the following line to the top of your code:

use warnings;

      

Or, if you already have one, you should pay attention to the warning messages you receive. Others have correctly pointed out that your input string has no literal pipes. I think you really want something like this:



use strict;
use warnings;

my @tmp_field_validation = ("         1          10       2009\n");

foreach my $line  (@tmp_field_validation)
{
        chomp $line;
        $line =~ s/^\s*//;
        my ($cycle_code,$cycle_month,$cycle_year)= split /\s+/, $line;
        print "$line\n";
        print "$cycle_code|$cycle_month|$cycle_year";
}

      

Outputs the following:

1          10       2009
1|10|2009

      

+4


a source


Because the whole line ended up in the $ cycle_code variable. The other two variables are empty strings.



+3


a source







All Articles