simple map query

J

John H

i have been trying to write very simple statement using map,
what it should do check the condition if it fails don't push it in to
result
@Myarray= map {if($_!~/echo/),@fromarray

i have totaly confused now , I have tried all the damm combination of
brackets , it does not work,
its pathatic now ,
 
P

phaylon

John said:
@Myarray= map {if($_!~/echo/),@fromarray

perldoc -f map has many examples, such as:

%hash = map { getkey($_) => $_ } @array;

therefore:

@myarray = map { $_ !~ /echo/ } @fromarray;

but I think you're searching perldoc -f grep

hth,phay
 
P

Paul Lalli

John H said:
i have been trying to write very simple statement using map,
what it should do check the condition if it fails don't push it in to
result
@Myarray= map {if($_!~/echo/),@fromarray

i have totaly confused now , I have tried all the damm combination of
brackets , it does not work,
its pathatic now ,

You're confusing the purpose of 'map' and 'grep'. map() returns a list
of all results of some block of code applied to each element of a list.
grep() returns a list of all elements of a list for which some condition
is true. You want to do the second one.

my @Myarray = grep {! /echo/} @fromarray;

read more about each of these functions in
perldoc -f map
and
perldoc -f grep

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,916
Members
47,458
Latest member
Chris#

Latest Threads

Top