开发者

Why do I get strange output from Perl using SQL?

开发者 https://www.devze.com 2022-12-29 03:25 出处:网络
Here is my Perl code: foreach my $line(@tmp_field_validation) { chomp $line; my ($cycle_code,$cycle_month,$cycle_year)= split /\\s*\\|\\s*/, $line;

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 is the output:

         1          10       2009
1           10       2009||

What's wrong over here? I expected the pipes to be between the variables. Why are the pipes getting printed after all the thr开发者_运维知识库ee variables?

EDIT: tmp_field_validation is the output of an sql query which has a select statement like :

select cycle_code,cycle_month,cycle_year from ....

so the output is coming as 3 different columns when i executed the query in TOAD. but the same query when used in this script how could it be possible that the output is considered as the single field cycle_code


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

use warnings;

Or, if your already have it there, you should pay attention to the warning messages you receive. Others have correctly pointed out that your input line does not have any 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


Because the entire line got stocked in the $cycle_code variable. The other two variables are empty strings.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号