using a function in map?

using a function in map?

am 15.11.2007 12:20:49 von bugbear

I'm doing this:

my @new_data = map { myfunc($_) } @old_data;

the body of myfunc is quite significant, and
this has worked well for me so far. The arrays
are arrays of complex data structures.

However, a change in requirment means that I now
what to be able to remove some of the elements
of old_data (in other words, not have them appear in @new_data).

But (as far as I can tell) there is NO value
returnable by myfunc that will do this for me.

If myfunc returns undef, I simply get an undef
in @new_data.

Advice on how to proceed welcome.

BugBear

Re: using a function in map?

am 15.11.2007 12:24:32 von Peter Makholm

bugbear writes:

> If myfunc returns undef, I simply get an undef
> in @new_data.

If undef otherwise is an invalid return valu you can just remove them
with grep. Then you will have something like:

my @new = grep { defined $_ } map { mynewfunc $_ } @old

//Makholm

Re: using a function in map?

am 15.11.2007 12:38:14 von Abigail

_
bugbear (bugbear@trim_papermule.co.uk_trim) wrote on VCLXXXIX September
MCMXCIII in :
"" I'm doing this:
""
"" my @new_data = map { myfunc($_) } @old_data;
""
"" the body of myfunc is quite significant, and
"" this has worked well for me so far. The arrays
"" are arrays of complex data structures.
""
"" However, a change in requirment means that I now
"" what to be able to remove some of the elements
"" of old_data (in other words, not have them appear in @new_data).
""
"" But (as far as I can tell) there is NO value
"" returnable by myfunc that will do this for me.
""
"" If myfunc returns undef, I simply get an undef
"" in @new_data.
""
"" Advice on how to proceed welcome.


If you want myfunc not to return anything, then don't return anything.

Noone is keeping a gun to your head to actually put an expression after
'return', is there?


Abigail
--
my $qr = qr/^.+?(;).+?\1|;Just another Perl Hacker;|;.+$/;
$qr =~ s/$qr//g;
print $qr, "\n";

Re: using a function in map?

am 15.11.2007 12:56:50 von Tad McClellan

bugbear wrote:
> I'm doing this:
>
> my @new_data = map { myfunc($_) } @old_data;
>
> the body of myfunc is quite significant, and
> this has worked well for me so far. The arrays
> are arrays of complex data structures.
>
> However, a change in requirment means that I now
> what to be able to remove some of the elements
> of old_data (in other words, not have them appear in @new_data).
>
> But (as far as I can tell) there is NO value
> returnable by myfunc that will do this for me.
>
> If myfunc returns undef, I simply get an undef
> in @new_data.
>
> Advice on how to proceed welcome.


return the empty list.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: using a function in map?

am 15.11.2007 13:07:04 von Paul Lalli

On Nov 15, 6:38 am, Abigail wrote:
> _
> bugbear (bugbear@trim_papermule.co.uk_trim) wrote on VCLXXXIX September
> MCMXCIII in :

> "" what to be able to remove some of the elements
> "" of old_data (in other words, not have them appear in
> "" @new_data).
> ""
> "" But (as far as I can tell) there is NO value
> "" returnable by myfunc that will do this for me.
> ""
> "" If myfunc returns undef, I simply get an undef
> "" in @new_data.

> If you want myfunc not to return anything, then don't return
> anything.
>
> Noone is keeping a gun to your head to actually put an expression
> after 'return', is there?

To add to what Abigail is saying: an empty 'return;' will return
undef in scalar context, but will return the empty list in a list
context. Contrasting, 'return undef;' will return undef in scalar
context, and a list containing undef in list context.

The map{} in your expression is evaluating your function in a list
context, and so if you want to return 'nothing', you need an empty
list. Hence, the empty 'return;' statement.

Paul Lalli

Re: using a function in map?

am 15.11.2007 13:29:33 von bugbear

Paul Lalli wrote:
> On Nov 15, 6:38 am, Abigail wrote:
>> _
>> bugbear (bugbear@trim_papermule.co.uk_trim) wrote on VCLXXXIX September
>> MCMXCIII in :
>
>> "" what to be able to remove some of the elements
>> "" of old_data (in other words, not have them appear in
>> "" @new_data).
>> ""
>> "" But (as far as I can tell) there is NO value
>> "" returnable by myfunc that will do this for me.
>> ""
>> "" If myfunc returns undef, I simply get an undef
>> "" in @new_data.
>
>> If you want myfunc not to return anything, then don't return
>> anything.
>>
>> Noone is keeping a gun to your head to actually put an expression
>> after 'return', is there?
>
> To add to what Abigail is saying: an empty 'return;' will return
> undef in scalar context, but will return the empty list in a list
> context. Contrasting, 'return undef;' will return undef in scalar
> context, and a list containing undef in list context.
>
> The map{} in your expression is evaluating your function in a list
> context, and so if you want to return 'nothing', you need an empty
> list. Hence, the empty 'return;' statement.

Perfect answer;

Thanks to both.

BugBear