Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Filtering array of strings and numbers

by AnomalousMonk (Archbishop)
on Apr 29, 2016 at 16:32 UTC ( [id://1161888]=note: print w/replies, xml ) Need Help??


in reply to Re: Filtering array of strings and numbers
in thread Filtering array of strings and numbers

Except perhaps for purposes of enhanced readability/maintainability, is the explicit stringification ever needed? Doesn't  ne or any other stringwise comparator implicitly stringify everything it operates on, including references (but with due deference to undef)? (Granted, stringifiying a reference for comparison purposes is usually useless, but that's another discussion.)

c:\@Work\Perl\monks>perl -wMstrict -le "use warnings; use strict; ;; use Data::Dump qw(dd); ;; my @stuff = ('abc', 123, 'ab4', '123', 456, [ 123 ], { 123 => 'x', x +=> 123 }); ;; my $thing = 123; my @filtered = grep { $thing ne $_ } @stuff; dd \@filtered; ;; $thing = '123'; @filtered = grep { $thing ne $_ } @stuff; dd \@filtered; " ["abc", "ab4", 456, [123], { 123 => "x", x => 123 }] ["abc", "ab4", 456, [123], { 123 => "x", x => 123 }]

OT: I sometimes see what one might call "super stringification" in code that often seems to originate from biological users, e.g.:
    my $filename = '...';
    open my $fh, '<', "$filename" or die "...";
Can anyone comment on the origin or history of this apparent (mis-)meme?

OT: Update: The other odd idiomatic usage I see that seems to be of biological origin is along the lines of:
    my $fh;
    unless (open $fh, ...) {
        print "open failed...";
        exit;
        }
Huh?!?   Are BioMonks constitutionally averse to die-ing? Is it that  exit; is needed to return a non-error exit code to the OS?


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Filtering array of strings and numbers
by Laurent_R (Canon) on Apr 29, 2016 at 17:01 UTC
    I had the same reaction. I would think that the ne, eq, etc. string relational operators all coerce their arguments into strings, so that no explicit stringification is needed. But I may miss something.

    Having said that, string comparison operators and arithmetic relational operator don't always return the same thing, so the OP should be aware of that. Remember that 10 is smaller than 3 for string relational operators.

      ... string relational operators all coerce their arguments into string, so that no explicit stringification is needed. But I may miss something.

      The OP uses, and Athanasius's reply explicitly assumes, an equality operator, so I don't think you're missing anything. If you are, I'm missing it too, and I'd be very interested to learn what it is. (Update: Ok, here's something I missed.)

      I agree with you that if you try to use numeric instead of string comparison, you're venturing out onto very thin ice indeed.

      Update: I'm posting this reply again because the first post does not seem to appear in its intended parent thread. I can see the first version of this reply (update: this node) in the history of my posts in my personal node, but there is no link to any parent; it seems to be freely floating somewhere in cyberspace.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1161888]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found