缺少数组输出中的第一行

时间:2017-01-17 10:43:43

标签: arrays perl loops foreach

当我尝试迭代SQL查询的某些结果时,我没有看到第一个条目。以下是在外部循环中,具有不同的查询输入。某些版本返回一行,其他版本返回十行,但第一行总是丢失。从“当前条目”开始的显示行始终从2开始。我知道Perl数组从零开始,但“foreach”不询问特定数字 - 只是为了显示每个数组元素。我在这里做了一些明显不对的事吗?

感谢。

  $OurSQL = "select DISTINCT BORROWER.BARCODE, POST_CODE, SURNAME, FIRST_NAMES, ";
  $OurSQL = $OurSQL."LOAN.CREATE_LOCATION FROM ADDRESS, CONTACT_POINT, BORROWER ";
  $OurSQL = $OurSQL."left join LOAN on (BORROWER.BORROWER_ID = LOAN.BORROWER_ID) ";
  $OurSQL = $OurSQL."where (BORROWER.BORROWER_ID = CONTACT_POINT.BORROWER_ID) and ";
  $OurSQL = $OurSQL."(CONTACT_POINT.ADDRESS_ID = ADDRESS.ADDRESS_ID) and ";
  $OurSQL = $OurSQL."(upper(BORROWER.SURNAME) = upper('".$sname."') and upper(BORROWER.FIRST_NAMES) = upper('".$fname."')) and ";
  $OurSQL = $OurSQL."(ADDRESS.POST_CODE = '".$postcode."') AND (LOAN.CREATE_DATE >";
  $OurSQL = $OurSQL."dateadd(year, -1, getdate())) ORDER BY POST_CODE, SURNAME, FIRST_NAMES, CREATE_LOCATION";

$lib = 0;
  (@our_usage) = &sql($d, $OurSQL);
  # Iterate through array returned for the current borrower
  foreach $entry (@our_usage)
  {
    # Put current line into our variables
    $lib++;
    print "Current entry ($lib): $entry\n";
    ($ourbcode, $ourpcode, $oursname, $ourfnames, $ourloc) = split('~', $entry);
    $ourline = "$ourbcode $ourpcode $oursname $ourfnames $ourloc";
    # Show each line on screen
    print $ourline."\n";
    print OUT $ourline."\n";
  } # End of results loop

1 个答案:

答案 0 :(得分:0)

我们没有足够的代码来查看到底出了什么问题。 这是一个完整的脚本,使用固定字符串而不是调用sql(),希望你可以将它转换为适合你的东西。

$ cat x.pl
#!/usr/bin/env perl

use warnings;
use diagnostics;
use strict;

# ...your other code here...

my @our_usage = ("a~b~c~d~e", "1~2~3~4~5", "R~S~T~U~V");
# Iterate through array returned for the current borrower
my $lib = 0;
foreach my $entry (@our_usage)
{
  # Put current line into our variables
  $lib++;
  print "Current entry ($lib): $entry\n";
  my ($ourbcode, $ourpcode, $oursname, $ourfnames, $ourloc) = split('~', $entry);
  my $ourline = "$ourbcode $ourpcode $oursname $ourfnames $ourloc";
  # Show each line on screen
  print $ourline."\n";
} # End of results loop

示例运行:

$ perl ./x.pl
Current entry (1): a~b~c~d~e
a b c d e
Current entry (2): 1~2~3~4~5
1 2 3 4 5
Current entry (3): R~S~T~U~V
R S T U V