Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Text::Unaccent::PurePerl undef values

by IB2017 (Pilgrim)
on Mar 18, 2018 at 19:50 UTC ( #1211209=perlquestion: print w/replies, xml ) Need Help??

IB2017 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I'm sorting an array deaccenting its elements with Text::Unaccent::PurePerl.

Everything is fine except that my array happens to have now and then some undef elements which apparently is not accepted by Text::Unaccent::PurePerl (Error: unac_string: Input character string is undefined). Any way to turn - on the run - these undef elements let's say, to an empty string ""? My array comes from reading a SQLite database. What would be the approach you suggest?

@$ResultsFinal =( sort { unac_string($a->[($OptOrderToDisplayTable)]) +cmp unac_string($b->[($OptOrderToDisplayTable)])} @$ResultsFinal );

Replies are listed 'Best First'.
Re: Text::Unaccent::PurePerl undef values
by choroba (Archbishop) on Mar 18, 2018 at 20:00 UTC
    With 5.10+, you can use the defined-or operator:
    @$ResultsFinal = sort { unac_string($a->[$OptOrderToDisplayTable] // " +") cmp unac_string($b->[$OptOrderToDisplayTable] // " +") } @$ResultsFinal );
    In older Perls, you need to be more verbose:
    unac_string( defined $a->[$OptOrderToDisplayTable] ? +$a->[$OptOrderToDisplayTable] : "")

    Update: Fixed the code. If you had provided SSCCE, I could've tested my code :-)

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thank you for your suggestion. Unfortunately it does not solve the problem. I do not quite understand the map you propose as to me it does not seem to apply to the elements of the array I use to sort. Here a dump of the @ResultsFinal (3 records) before and after the sorting. If sorting for $OptOrderToDisplayTable=4, for example, you can see that the corresponding element of $VAR3 still remains undef . This fires an error by unac_string . Am I overseeing something in your code?

      $VAR1 = [ 841, 'bbb', '', 'bbb', 'bbb', undef, undef, undef, '', '' ]; $VAR2 = [ 842, 'bbb', '', 'ccc', 'aaa', undef, undef, undef, '', '' ]; $VAR3 = [ 850, 'bbb', '', 'hhh', undef, undef, undef, undef, '', '' ]; ###After $VAR1 = [ 841, 'bbb', '', 'bbb', 'bbb', undef, undef, undef, '', '' ]; $VAR2 = [ 842, 'bbb', '', 'ccc', 'aaa', undef, undef, undef, '', '' ]; $VAR3 = [ 850, 'bbb', '', 'hhh', undef, undef, undef, undef, '', '' ];
        Sorry, fixed.
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thank you. This works like a charm. You use some operators I never used before :( so I learned something new too. Sorry for not having posted a complete example with data. Next time I'll certainly do.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1211209]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2020-11-27 21:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?