Using grep on undefined array

Mark Overmeer mark at overmeer.net
Wed Aug 14 12:43:18 BST 2013


* Matt Freake (matthew.d.freake at gmail.com) [130814 08:01]:
> On Wed, Aug 14, 2013 at 8:40 AM, Mark Overmeer <mark at overmeer.net> wrote:
> > * Andrew Beverley (andy at andybev.com) [130813 23:24]:
> > > Could someone please explain to me why the following outputs an empty
> > > string rather than "*"?
> >
> > @fields != @$fields, which is causing the confusion.
> 
> I don't think the original poster is confusing those two, and when I
> applied 'use strict' to the original code (with the sub) it didn't help (no
> warnings or errors).

I my need to explain my reply:

>> {   my $fields = shift;
>>     my @fields = grep $_ ne 'domain', @$fields;
>>     my $select_fields = $fields ? join(',', map { 'users.' . $_
>> } @fields) : '*';

It is not unusual to re-use names when they refer to the same information,
or very simular info.  @ARGV $ARGV, etc   But here, $fields is the list
maybe containing 'domain', and @fields is without it.  Now, to get Andrews
unexpected behavior of his question nr 1, you feed the function with
an array which only contains 'domain'.  Now, the $fields is true and
the @fields is empty. An empty string is produced.  I expect he wants
to get a '*' in this case.  So, my wild guess is that the fix to the
algorithm is to change "$fields ?" into "@fields ?"  And better to rename
one of both to reflect their different content.
-- 
Greetings from YAPC::EU Kiev,

               MarkOv

------------------------------------------------------------------------
       Mark Overmeer MSc                                MARKOV Solutions
       Mark at Overmeer.net                          solutions at overmeer.net
http://Mark.Overmeer.net                   http://solutions.overmeer.net



More information about the london.pm mailing list