text: scratchpad vinoth.ree <p> 1.Count array elements that match a pattern In a scalar context, grep returns a count of the selected elements. </p> <c>my @fruits = ('apple','orange','apple','orange'); my $num_apple = grep /^apple$/i, @fruits print $num_apple; </c> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=</p> <p>2.Extract unique elements from a list</p> <c> @unique = grep { ++$count{$_} < 2 } qw(a b a c d d e f g f h h);print "@unique\n"; </c> <p>a b c d e f g h</p> <p>The <c>$count{$_}</c>.</p> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=</p> <p>3.Extract list elements that occur exactly twice</p> <c>my @crops = qw(wheat corn barley rice corn soybean hay alfalfa rice hay beets corn hay); my @dupes = grep { $count{$_} == 2 } grep { ++$count{$_} > 1 } @crops;print "@dupes\n";</c> <p> The second argument to grep is "evaluated in a list context" before the first list element is passed to grep's BLOCK or EXPR. This means that the grep on the right completely loads the %count hash before the grep on the left begins evaluating its BLOCK. </p> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=</p> <p> 4.List text files in the current directory </p> <c> @files = grep { -f and -T } glob '* .*'; print "@files\n"; </c> <p>. </p> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=</p> <p> 5.Transform filenames to file sizes <c>@sizes = map { -s $_ } @file_names;</c> <p> The -s file test operator returns the size of a file and will work on regular files, directories, special files, etc. </p> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=</p> <p> 6.Generate a random String. In this example the values of the elements in map's input LIST are irrelevant; only the number of elements in LIST affects the result. <c>@a = (0 .. 9, 'a' .. 'z'); $password = join '', map { $a [int rand @a ] } 0 .. 7; print "$password\n"; </c> </p> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=</p> <p> 7.Sort by reverse order To reverse the sort order, reverse the position of the operands of the cmp or <=> operators: <c>sort { $b <=> $a } @array;</c> or change the sign of the block's or subroutine's return value: <c>sort { -($a <=> $b) } @array;</c> or add the reverse function (slightly less efficient, but perhaps more readable): <c>reverse sort { $a <=> $b } @array;</c> </p> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=</p> <p>: <c> </c> <h4>Numerical Sort..</h4> <c> my %data = (bananas => 1,oranges => 7,apples => 12, mangoes => 3,pears => 8,); # Using <=> instead of cmp because of the numbers foreach my $fruit (sort {$data{$a} <=> $data{$b}} keys %data) { print $fruit . ": " . $data{$fruit} . "\n"; } </c> <b>How word boundry works?</b> <p.</p> <p>\b cannot match at the position between the T and the h. It cannot match between the h and the i either, and neither between the i and the s.</p> <p>The next character in the string is a space. \b matches here because the space is not a word character, and the preceding character is. Again, the engine continues with the i which does not match with the space.</p> <p.</p> <p.</p> <p>The engine has successfully matched the word is in our string, skipping the two earlier occurrences of the characters i and s. If we had used the regular expression is, it would have matched the is in This. </p> <p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <h4>Printing Indian Rupee Symbol:</h4> <c>use strict; use warnings; use Data::Dumper; use Encode qw(encode); my $rupee = encode("UTF-8", chr(0x20B9)); print "\n$rupee"."19264/-\n"; </c> <p> <h4>How to list the available functions from a module.</h4> <c> BEGIN { no strict 'refs'; print Dumper(\%{"main::YAML::XS::"}); exit; }; </c> <p <h4> Dynamically load module.</h4> If you don't know the name of the module until runtime you'll have to do the translation between module name (My::Module) and file name (My/Module.pm) manually: <c> my $module = 'My::Module'; eval { (my $file = $module) =~ s|::|/|g; require $file . '.pm'; $module->import(); 1; } or do { my $error = $@; # ... }; </c> </p> <p> Index Implementation. </p> <b>Single Match:</b> <c> sub match_positions { my ($regex, $string) = @_; return if not $string =~ /$regex/; return ($-[0], $+[0]); } </c> <b>Multiple Match:</b> <c> sub match_all_positions { my ($regex, $string) = @_; my @ret; while ($string =~ /$regex/g) { push @ret, [ $-[0], $+[0] ]; } return @ret } </c> <b>Linux Stuff:</b> <b>1.How to kill all processes run by a user on Linux ?</b><br> <c>A.ps -ef | grep vinoth | awk '{ print $2 }' | sudo xargs kill -9</c><br> <c>B.pgrep -u vinoth | sudo xargs kill -9</c><br> <c>C.sudo pkill -u vinoth </c>