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> is a single element of a Perl hash, which is a list of key-value pairs. The hash keys are the elements of grep's input list; the hash values are running counts of how many times an element has passed through grep's BLOCK. The expression is false for all occurrences of an element except the first.</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> The glob function emulates Unix shell filename expansions; an asterisk means "give me all the files in the current directory except those beginning with a period". The -f and -T file test operators return TRUE for plain and text files respectively. Testing with -f and -T is more efficient than testing with only -T because the -T operator is not evaluated if a file fails the less costly -f test. </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> 8.Sort Hash by values. Unlike hash keys, hash values are not guaranteed to be unique. If you sort a hash by only its values, the sort order of two elements with the same value may change when you add or delete other values. To do a stable sort by hash values, do a primary sort by value and a secondary sort by key: <c>%hash = ( Elliot => Babbage, Charles => Babbage, Grace => Hopper, Herman => Hollerith ); @sorted = map { { ($_ => $hash{$_}) } } sort { $hash{$a} cmp $hash{$b} or $a cmp $b } keys %hash; foreach $hashref (@sorted) { ($key, $value) = each %$hashref; print "$key => $value\n"; } Charles => Babbage Elliot => Babbage Herman => Hollerith Grace => Hopper </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>Let's see what happens when we apply the regex \bis\b to the string This island is beautiful. The engine starts with the first token \b at the first character T. Since this token is zero-length, the position before the character is inspected. \b matches here, because the T is a word character and the character before it is the void before the start of the string. The engine continues with the next token: the literal i. The engine does not advance to the next character in the string, because the previous regex token was zero-width. i does not match T, so the engine retries the first token at the next character position.</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>Advancing a character and restarting with the first regex token, \b matches between the space and the second i in the string. Continuing, the regex engine finds that i matches i and s matches s. Now, the engine tries to match the second \b at the position before the l. This fails because this position is between two word characters. The engine reverts to the start of the regex and advances one character to the s in island. Again, the \b fails to match and continues to do so until the second space is reached. It matches there, but matching the i fails.</p> <p>But \b matches at the position before the third i in the string. The engine continues, and finds that i matches i and s matches s. The last token in the regex, \b, also matches at the position before the third space in the string because the space is not a word character, and the character before it is.</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/ 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> -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>