【问题标题】:How to limit number of lines in Perl如何限制 Perl 中的行数
【发布时间】:2015-12-13 21:33:04
【问题描述】:

我有一个 perl 子例程,它正在 PDF 报告上从 oracle 表中打印文本,我想将它写入的行数限制为 4。这是子例程:

sub printTextToPDF{
  my ($left_pos, $line_height, $case_field) = @_;
  my ($field_str, $line_pos) = ('', 0);

  if($case_field =~ /(.*?)\\+$/) {
    $case_field = $1;
  }

  my @field = split /\s/, $case_field;

    for $cnt (0 .. $#field) {
      $field_str .= $field[$cnt];

      if($cnt != (scalar @field - 1)) {
        $line_pos = (rindex($field_str, "\n") > 0) ? rindex($field_str, "\n") : 0;

        my ( $strwidth1 ) = prStrWidth(substr($field_str, $line_pos),'Courier',5);
        my ( $strwidth2 ) = prStrWidth($field[$cnt+1],'Courier',5);
        my ( $totalwidth ) = $strwidth1 + $strwidth2;

        $field_str .= ($totalwidth < 124) ? ' ' : "\n";
      }
    }

    @field = split("\n", $field_str);

    for $pos (0 .. $#field) {
      prText($left_pos, $line_height + (4.5*(scalar @field - ($pos+1))), $field[$pos]);
  }
}

我目前正在使用oracle SQL中的substr函数来限制字符数,但这并不是限制行数的最佳方法。有没有办法我可以做到,如果有,请告诉我如何。提前致谢。

【问题讨论】:

  • 在你的Oracle查询中,可以where rownum &lt;= 4吗?
  • @Hambone - 这只会限制行数。我的字符串将存储在一行中,我需要将其打印在 PDF 报告上并限制长度不超过提供的空间。
  • 明白了。我很想看看@SteveCarlson 的解决方案是否能满足您的需求。如果不是,请澄清。我很确定这里有一个解决方案

标签: oracle perl pdf


【解决方案1】:

这对你有用吗?

$count = 0;
for $pos (0 .. $#field) 
    {
    prText($left_pos, $line_height + (4.5*(scalar @field - ($pos+1))), $field[$pos]);
    $count++
    if ($count >= 4)
        {
        last;
        }
    }

【讨论】:

  • 如果我们在打高尔夫球,我们可以将最后 5 行替换为 last if $count++ &gt; 3
猜你喜欢
  • 1970-01-01
  • 2010-09-09
  • 2011-04-27
  • 2012-08-23
  • 2023-03-27
  • 1970-01-01
  • 2010-10-07
  • 2013-04-17
  • 2019-02-28
相关资源
最近更新 更多