in reply to Re: way of declare an integer variable instead use /^\d+$/
in thread way of declare an integer variable instead use /^\d+$/

my $x :Int = VALUE is exactly the sugar I want. I use
my ($text, $page) = split "\t", $line;
in my addpdfbookmark.pl tool, $line is got from ocr, set '9O' to '90', I was very confused when
warn $text, ' on page ', $page, ' must be wrong' if $page < $lastpage;
was triggered, I have to write a valid subroutine,
sub valid($){ my ($text, $page) = split "\t", shift; $page =~ /^\d+$/ or die 'page not numbers'; warn $text, ' on page ', $page, ' must be wrong' if $page < $lastp +age; $lastpage = $page; return ($text, $page); }
, It's better to declare it as an integer, But I got 'Invalid SCALAR attribute' error, use what can make it work?

Replies are listed 'Best First'.
Re^3: way of declare an integer variable instead use /^\d+$/
by LanX (Saint) on May 25, 2024 at 22:14 UTC
    So your OCR is confusing O and 0 ?

    Typing to integer won't fix that, you will need better OCR or hope you can cover the problems with heuristics.

    > Invalid SCALAR attribute' error,

    Because you need to implement it first, following the links I gave.

    Better don't

    That was an XY problem and implementing this would be a total overkill. :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      The ocr is named cnocr, it is the best I can get, It's trained specific for chinese under python, it's much accurate compared with tesseract.

      'Because you need to implement it first, following the links I gave.'???

      where is the link about the sugar? is it not simple as add 'use ...'? I'm using 5.38 version under arch.declare a integer can be a very useful method in all ways.

Re^3: way of declare an integer variable instead use /^\d+$/
by BillKSmith (Monsignor) on May 26, 2024 at 19:49 UTC
    It probably does not matter if the O/0 error is in the original .pdf file or if the OCR misreads it. The tr operator (refer tr/SEARCHLIST/REPLACEMENTLIST/cdsr) directly corrects the problem. Your test for a reasonable value is probably a good idea, but it really has nothing to do with the O/0 problem.
    my ($text, $page) = split "\t", $line; $page =~ tr/O/0/ and warn "Replaced 'O' with '0' in page $page";
    Bill