Sei sulla pagina 1di 13

7/5/2016

FilteringvaluesusingPerlgrep

Maven (http://perlmaven.com/advanced-perl-maven-video-course)

Advanced Perl Maven video course (/advanced-perl-maven-video-course)

Libraries and Modules, Packages and Namespaces


1. Introduction to the Advanced Perl course (pro) (/pro/introduction-to-advanced-perl-course)
2. Perl 4 libraries (pro) (/pro/perl4-libraries)
3. The problem with libraries (pro) (/pro/the-problem-with-libraries)
4. Namespaces and packages (pro) (/pro/namespaces-and-packages)
5. Modules in Perl (pro) (/pro/modules)
6. How does require nd the module to be loaded? (pro) (/pro/require-at-inc)
7. What is the difference between require and use? What does import do? (pro) (/pro/use-requireimport)
8. Exporting and importing functions easily (pro) (/pro/import)
9. Restrict the import by listing the functions to be imported (pro) (/pro/restrict-the-import)
10. Import on demand (pro) (/pro/on-demand-import)
11. Modules - Behind the scenes (pro) (/pro/behind-the-scenes)
12. Tools to package Perl scripts, modules, and applications (pro) (/pro/tools-to-packagemodules)
13. Distribution directory layout (/distribution-directory-layout)
14. Makele.PL of ExtUtils::MakeMaker (pro) (/pro/makele-pl-of-extutils-makemaker)
15. Makele.PL of Module::Install (pro) (/pro/makele-pl-of-module-install)
16. Build.PL of Module::Build (pro) (/pro/build-pl-of-module-build)
http://perlmaven.com/filteringvalueswithperlgrep

1/13

7/5/2016

FilteringvaluesusingPerlgrep

17. Changes and README (pro) (/pro/changes-and-readme)


18. MANIFEST and MANIFEST.SKIP (pro) (/pro/manifest-and-manifest-skip)
19. Packaging a script and a module (pro) (/pro/le-and-module)
20. Packaging with Makele.PL (pro) (/pro/packaging-with-makele-pl)
21. Packaging with Build.PL (pro) (/pro/packaging-with-build-pl)
22. Test le (pro) (/pro/test-le)
23. How to create a Perl Module for code reuse? (/how-to-create-a-perl-module-for-code-reuse)
References
1. Passing two arrays to a function (/passing-two-arrays-to-a-function)
2. Array references in Perl (/array-references-in-perl)
3. Static and state variables in Perl (/static-and-state-variables-in-perl)
4. Filtering values using Perl grep (/ltering-values-with-perl-grep)
5. Transforming a Perl array using map (/transforming-a-perl-array-using-map)
Object Oriented Programming in Perl using raw classes
1. Core Perl OOP: Constructor (pro) (/pro/core-perl-oop-constructor)
2. Core Perl OOP: attributes, getter - setter (pro) (/pro/core-perl-oop-getter-setter)
3. What should setters return? (Examples with core Perl OOP) (pro) (/pro/what-should-settersreturn)
4. Core Perl OOP: Constructor arguments (pro) (/pro/core-perl-oop-constructor-arguments)
Some other advanced topics
1. Always use strict and use warnings in your perl code! (/always-use-strict-and-use-warnings)
2. How to capture and save warnings in Perl (/how-to-capture-and-save-warnings-in-perl)
3. use diagnostic; or use splain (/use-diagnostics-or-splain)
4. Splice to slice and dice arrays in Perl (/splice-to-slice-and-dice-arrays-in-perl)
5. AUTOLOAD - handling Undened subroutines (/autoload)
6. BEGIN block - running code during compilation (/begin)
7. END block - running code after the application has ended (/end)
8. How to sort faster in Perl? (using the Schwartzian transform) (/how-to-sort-faster-in-perl)
9. $_ the default variable of Perl (/the-default-variable-of-perl)

Filtering values using Perl grep


http://perlmaven.com/filteringvalueswithperlgrep

2/13

7/5/2016

FilteringvaluesusingPerlgrep

,
grep (/search/grep)

lter (/search/lter)

List::MoreUtils (/search/List::MoreUtils)

any (/search/any)
<> (/search/<>)

glob (/search/glob)

Prev (/static-and-state-variables-in-perl)
Next (/transforming-a-perl-array-using-map)
The internal grep function
of Perl is a lter. You give it
a list of values and a condition, and it returns a sublist of values that yield true for the given
condition. It is a generalization of the grep or egrep commands we know from UNIX and Linux, but
you don't need to know these commands in order to understand the grep of Perl.
The grep function takes two arguments. A block and a list of values.
For every element of the list, the value is assigned to $_ , the default scalar variable of Perl (/thedefault-variable-of-perl), and then the block is executed. If the return value of the block is false , the
value is discarded. If the block returned true the value from the list is kept as one of the return
values.
Pay attention, there is no comma between the block and the second parameter!
Let's see a few examples for grep:

Filter out small numbers


1.my@numbers=qw(825317);
2.my@big_numbers=grep{$_>4}@numbers;
3.print"@big_numbers\n";#(8,5,7)
This grep passes the values that are greater than 4, ltering out all the values that are not greater
than 4.

Filter out the new les


1.my@files=glob"*.log";
2.my@old_files=grep{M$_>365}@files;
3.printjoin"\n",@old_files;
glob "*.log" will return all the les with a .log extension in the current directory.
-M $path_to_le returns the number of days passed since the le was last modied.
http://perlmaven.com/filteringvalueswithperlgrep

3/13

7/5/2016

FilteringvaluesusingPerlgrep

This example lters out the les that have been modied within the last year, and only let's through
les that are at least one year old.

Is this element in the array?


Another interesting use of grep is to check if an element can be found in an array. For example, you
have a list of names and you would like to know if the given name is in the list?
1.usestrict;
2.usewarnings;
3.
4.my@names=qw(FooBarBaz);
5.my$visitor=<STDIN>;
6.chomp$visitor;
7.if(grep{$visitoreq$_}@names){
8.print"Visitor$visitorisintheguestlist\n";
9.}else{
10.print"Visitor$visitorisNOTintheguestlist\n";
11.}
In this case the grep function was placed in SCALAR context (/scalar-and-list-context-in-perl). In
SCALAR context grep will return the number of elements that went through the lter. As we are
checking if the $visitor equals to the current element this grep will return the number of times that
happens.
If that's 0, the expression will evaluate to false, if it is any positive number then it will evaluate to true.
This solution works, but because it depends on the context it might be unclear to some people. Let's
see another solution using the any function of the List::MoreUtils
(https://metacpan.org/pod/List::MoreUtils) module.

Do any of the elements match?


The any function has the same syntax as grep , accepting a block and a list of values, but it only
returns true or false. True, if the block gives true for any of the values. False if none of them match. It
also short circuits so on large lists this can be a lot faster.
1.useList::MoreUtilsqw(any);
2.if(any{$visitoreq$_}@names){
3.print"Visitor$visitorisintheguestlist\n";
4.}else{
5.print"Visitor$visitorisNOTintheguestlist\n";
6.}

UNIX grep and Linux grep?

http://perlmaven.com/filteringvalueswithperlgrep

4/13

7/5/2016

FilteringvaluesusingPerlgrep

UNIX grep and Linux grep?


Just to make the explanation round:
I mentioned that the build in grep function of Perl is a generalization of the UNIX grep command.
The UNIX grep lters the lines of a le based on a regular expression.
Perl's grep can lter any list of value based on any condition.
This Perl code implements a basic version of the UNIX grep:
1.my$regex=shift;
2.printgrep{$_=~/$regex/}<>;
The rst line gets the rst argument from the command line which should be a regular expression.
The rest of the command line arguments should be lenames.
The diamond operator <> fetches all the rows from all the les on the command line. The grep
lters them according to the regular expression. The ones that pass the ltering are printed.

grep on Windows
Windows does not come with a grep utility but you can install one or you can use the same Perl
script as above.

Prev (/static-and-state-variables-in-perl)
Next (/transforming-a-perl-array-using-map)

Written by
Gabor Szabo (https://plus.google.com/102810219707784087582?
rel=author)

Comments
http://perlmaven.com/filteringvalueswithperlgrep

5/13

7/5/2016

FilteringvaluesusingPerlgrep

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for
indentation.
25Comments

PerlMaven

Recommend 2

Share

Login

SortbyOldest

Jointhediscussion
ChrisJack 4yearsago

Theproblemwithusinggreptocheckforexistanceisit'snotespeciallyefficient.Iftheelementis
foundnearthebeginning,grepwillstillcontinuetotheend.Afastersolutionwouldstopwhenthe
elementisfound.
1

Reply Share

GaborSzabo

Mod >ChrisJack

4yearsago

Ifreallythatcausessomeslownessinaparticularprogramcanbedeterminedbyusinga
profiler,butthe"any"functionsolvesthisslowness.

Reply Share

LardFarnwell>GaborSzabo 4yearsago

Whatisthis"any"functionyouspeakof?
IfeellikeI'mmissingout:(
Ithoughtthatperlmightoptimizethisoutifit'sinanifstatement.
[EDIT]someresearchherefrom07http://mail.pm.org/pipermail/m...
Ifwelookathttp://rosettacode.org/wiki/Qu...
quick_sort(grep$_<$p,@a),$p,quick_sort(grep$_>=$p,@a)
Thepersonwewrotethismusthavebeenassumingthatgrepwasoptimizinghere
otherwiseitisvery
misleading.Ifnotitwouldbedoublingtheamountofcomparisonsneeded:(.

Reply Share

GaborSzabo

Mod >LardFarnwell

4yearsago

Indeedthatcodecompareseachvaluetwice.Itdoesnotchangethe
complexityofthealgorithmsoitonlymattersifthecomparisonisexpensive.

Reply Share

random.engineer>GaborSzabo 2yearsago

Cantweusefollowingtechniquetofindavalueinalist?Ithinkthisisfasterthanusing
grep.
my@names=qw(FooBarBaz)
my%hash
@hash{@names}=undef
http://perlmaven.com/filteringvalueswithperlgrep

6/13

7/5/2016

FilteringvaluesusingPerlgrep

if(exists$hash{$value}){
#codegoeshere
}
1

Reply Share

GaborSzabo

Mod >random.engineer

2yearsago

Formultiplesearches/lookupitwillbefasterthangrep,butitdoesnotmatter
forsmallarrays.
1

Reply Share

bonnyvora 3yearsago

HiGabor,
Thankyousomuchforthiswonderfulinformativewebsite.
Ihaveonedoubt:
Howtomatchadirectorywithitsname?
Suppose,Iwanttoseeisthereanydirectorywithname"abc".
Itriedthis:
$name="abc"
if(d$file)
{
@files2=grep{/$name/}$file#$fileispointingtocurrentdirectory
}
__________________________________________________________________
Butthisalsomatchesthedirectorywithname"abc"aswellas"abc_99","abc00"etc
Iwanttomatchonlyforabc.
Canyoupleasehelpme?

Reply Share

GaborSzabo

Mod >bonnyvora

3yearsago

Iamnotsurewhydoyouusegrepherewhenyouonlyhaveonevaluetocheck?Whynot
just
if(d$fileand$fileeq$name)
or,ifyou'dliketocheckif$nameisonepartofthepaththen\bcanbeofhelphere:
if(d$fileand$file=~/\b$name\b/)
Thoughifyouhad$name="abc.def"thenthiswouldalsomatch"abcXdef"soyoumightwant
tomakesurenoneoftheregexmetacharactersworksin$name.
Check"perldocfquotemeta"forthis.

Reply Share

Vinod>GaborSzabo 2yearsago

HiGabor,
ihavethefollowingcontentsinarray:
@arr=qw(/tftpboot/:boot/tftpboot/boot:grub/tftpboot/boot/grub:pxegrub)
http://perlmaven.com/filteringvalueswithperlgrep

7/13

7/5/2016

FilteringvaluesusingPerlgrep

@arr=qw(/tftpboot/:boot/tftpboot/boot:grub/tftpboot/boot/grub:pxegrub)
ifilteredthecandidateswhicharenothavingaslash:"/"
@candidateshavingthecontents"bootgrubpxegrub"
nowiwantonlythoseelements/candidateswhichdon'tappearin@arrwithaslash
"/".i.e.,aspertheabovecontentsitshouldbe"pxegrub".
iusedgrephere,butnotgettingthecorrectoutput.
Usingthefollowingapproach:
foreachmy$c(@candidates){
my$pattern='\'.$c
if(grep{$pattern=~$_}@arr){
print"$cisnotrequired\n"
}
esle{
print"$cistherequiredcandidate\n"
}
}
Itsgiving"$cisnotrequired"forallthe@candidates.Plssuggest...
Thanks,
Vinod.

Reply Share

GaborSzabo

Mod >Vinod

2yearsago

probably:
grep{$_=~m{$pattern}}

Reply Share

moshenahmias 3yearsago

Hi,
justreadthisandaftercheckingthecodeinthe"Isthiselementinthearray?"sectionIfoundoutthat
thegrepfunctionreturnsthematchingname(underperl5.14),butyousaythatitreturnsanumber,
whatisgoingonhere?

Reply Share

GaborSzabo

Mod >moshenahmias

3yearsago

HaveyoucopiedtheexampleIhad,orhaveyoutriedtowrite
printgrep{...}?
Inthiscase,grepisinLISTcontextandthusitreturnstherealcontent,andnotthenumberof
elements.

Reply Share

Romina 3yearsago

HelloGabor,
http://perlmaven.com/filteringvalueswithperlgrep

8/13

7/5/2016

FilteringvaluesusingPerlgrep

HelloGabor,

Itriedalottomatcharegexpwithmore"++++"
Ihavethecase:
Lineis****file_size:25time:2013/06/1314:22:16file_name:home/sparse_dir/dir3/file1
delta_size:0itemize_change:>f.st....*****
Lineis****file_size:61time:2013/06/1314:22:16file_name:home/sparse_dir/dir3/file2
delta_size:0itemize_change:>f+++++++*****
Lineis****file_size:4096time:2013/06/1314:22:16file_name:
home/sparse_dir/dir3/test_dir1delta_size:0itemize_change:.d..t....*****
Lineis****file_size:11time:2013/06/1314:22:16file_name:
home/sparse_dir/dir3/test_dir1/file3delta_size:0itemize_change:>f+++++++*****
Lineis****file_size:43time:2013/06/1314:22:16file_name:
home/sparse_dir/dir3/test_dir1/file7delta_size:0itemize_change:>f+++++++*****
seemore

Reply Share

GaborSzabo

Mod >Romina

3yearsago

*means0ormore.+means1ormore,soIguessyouneed\++intheregex,or\+{4,}if
you'dliketomatch4ormore.

Reply Share

Romina>GaborSzabo 3yearsago

Thankyouverymuch.
Itworks:)

Reply Share

Perlito 3yearsago

CanweusegrepintheexamplebelowthisoutputisreturnedbyDumper(Data::Dumper)howdoI
retrievelastname(carmen)andfirstname(luis)??
$var1=bless({'name'=>'firstname'=>'John','lastname'=>'Carmen',....

Reply Share

GaborSzabo

Mod >Perlito

3yearsago

Thisisablessedreferencetoahash.Theclassshouldhavemethodstoretreivelastname
andfirstname.

Reply Share

Perlito>GaborSzabo 3yearsago

Doyouhaveanysimilarexamplepostedsomewhere?Thanks:)
1

http://perlmaven.com/filteringvalueswithperlgrep

Reply Share
9/13

7/5/2016

FilteringvaluesusingPerlgrep

MichaStolarczyk ayearago

Isthereapossibilitytochangeregexineachloopiteration?eg.
grep($_=~/^$unique_f[$k]/,@f)

.Willitlookforappropriateitemsin@farraywhen$kisbeingchanged?
Itdoesn'tseemtoworkformeusingthatpieceofcode.

Reply Share

GaborSzabo

Mod >MichaStolarczyk

ayearago

Whatwasyourfullcode?

Reply Share

MichaStolarczyk>GaborSzabo ayearago
my$aaa=<$fh>;

$aaa=~tr/\>[]'\\\///d;

my@f=split",",$aaa;

my@unique_f=uniq@f;

my$length_f=scalar@f;

print"\n\n\t$filename\nUniqueaa:@unique_f\n\nUniqueaacount:$unique_f_length

for(my$k=0;$k<=$unique_f_length;$k++){

my@foo;

@foo=grep(qr/^$unique_f[$k]/,@f);

my@foo_per;

@foo_per=((length@foo)/$length_f)*100;

print"$unique_f[$k]:@foo_per%\n";

Reply Share

GaborSzabo

Mod >MichaStolarczyk

ayearago

anddoyouhaveusestrictandusewarningsinthiscode?Ithinkyourcode
shouldwork,butifIwereyou,I'dwriteasmall,selfcontainedexemple
(withoutthereadingfromafile)toseehowdoesthiswork.

http://perlmaven.com/filteringvalueswithperlgrep

10/13

7/5/2016

FilteringvaluesusingPerlgrep

(withoutthereadingfromafile)toseehowdoesthiswork.

Reply Share

MichaStolarczyk>GaborSzabo ayearago

Yes,Ihaveusestrictandwarningsinmycode.
IneachiterationIgetsuchawarning:

Useofuninitializedvaluewithin@unique_finregexpcompliationatbottlenecks.p

Reply Share

GaborSzabo

Mod >MichaStolarczyk

ayearago

Itseemstheforloopshasanextraiteration.youprobablyneedtouse<
insteadof<=intheconditionoftheforloop.

Reply Share

MichaStolarczyk>GaborSzabo ayearago

Unfortunatelyit'snotthesolution.Thereisaproblemwith@unique_farray

Reply Share

Author: Gabor Szabo

Gabor provides training and development services. He loves to help people improve their way of
programming. He likes to write automated tests and refactor code.
He runs the Perl Weekly (http://perlweekly.com/) newsletter.
Gabor also runs the Perl Maven site.
Suggest a change

(https://github.com/szabgab/perlmaven.com/tree/main/sites/en/pages//ltering-values-with-perlgrep.txt)

http://perlmaven.com/filteringvalueswithperlgrep

11/13

7/5/2016

FilteringvaluesusingPerlgrep

(http://perlmaven.com/digitalocean)

http://perlmaven.com/filteringvalueswithperlgrep

12/13

7/5/2016

FilteringvaluesusingPerlgrep

Related articles
Scalar and List context in Perl, the size of an array (/scalar-and-list-context-in-perl)
$_ the default variable of Perl (/the-default-variable-of-perl)

English (http://perlmaven.com/ltering-values-with-perl-grep)
( http://he.perlmaven.com/ltering-values-with-perl-grep)
(http://tw.perlmaven.com/ltering-values-with-perl-grep)
(http://cn.perlmaven.com/ltering-values-with-perl-grep)

about the translations (http://perlmaven.com/about#translations)

http://perlmaven.com/filteringvalueswithperlgrep

13/13

Potrebbero piacerti anche