Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

vinoth.ree's scratchpad

by vinoth.ree (Monsignor)
on Jan 05, 2009 at 10:15 UTC ( #734135=scratchpad: print w/replies, xml ) Need Help??

1.Count array elements that match a pattern In a scalar context, grep returns a count of the selected elements.

my @fruits = ('apple','orange','apple','orange'); my $num_apple = grep /^apple$/i, @fruits; print $num_apple."\n";


2.Extract unique elements from a list

@unique = grep { ++$count{$_} < 2 } qw(a b a c d d e f g f h h);print +"@unique\n";

a b c d e f g h

The $count{$_} 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.


3.Extract list elements that occur exactly twice

my @crops = qw(wheat corn barley rice corn soybean hay alfalfa rice ha +y beets corn hay); my @dupes = grep { $count{$_} == 2 } grep { ++$count{$_} > 1 } @crops; +print "@dupes\n";

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.


4.List text files in the current directory

@files = grep { -f and -T } glob '* .*'; print "@files\n";

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.


5.Transform filenames to file sizes @sizes = map { -s $_ } @file_names;

The -s file test operator returns the size of a file and will work on regular files, directories, special files, etc.


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.

@a = (0 .. 9, 'a' .. 'z'); $password = join '', map { $a [int rand @a ] } 0 .. 7; print "$password\n";


7.Sort by reverse order To reverse the sort order, reverse the position of the operands of the cmp or <=> operators: sort { $b <=> $a } @array; or change the sign of the block's or subroutine's return value: sort { -($a <=> $b) } @array; or add the reverse function (slightly less efficient, but perhaps more readable): reverse sort { $a <=> $b } @array;


8.What's the difference between dynamic and lexical (static) scoping? Between local() and my()? local($x) saves away the old value of the global variable $x and assigns a new value for the duration of the subroutine which is visible in other functions called from that subroutine. This is done at run-time, so is called dynamic scoping. local() always affects global variables, also called package variables or dynamic variables. my($x) creates a new variable that is only visible in the current subroutine. This is done at compile-time, so it is called lexical or static scoping. my() always affects private variables, also called lexical variables or (improperly) static(ly scoped) variables. For instance:
sub visible { print "var has value $var\n"; } sub dynamic { local $var = 'local'; # new temporary value for the still-global visible(); # variable called $var } sub lexical { my $var = 'private'; # new private variable, $var visible(); # (invisible outside of sub scope) } $var = 'global'; visible(); # prints global dynamic(); # prints local lexical(); # prints global
Notice how at no point does the value "private" get printed. That's because $var only has that value within the block of the lexical() function, and it is hidden from the called subroutine. In summary, local() doesn't make what you think of as private, local variables. It gives a global variable a temporary value. my() is what you're looking for if you want private variables.


9.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:
%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

Numerical Sort..

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"; }
How word boundry works?

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.

\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.

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.

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.

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.

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.


Printing Indian Rupee Symbol:

use strict; use warnings; use Data::Dumper; use Encode qw(encode); my $rupee = encode("UTF-8", chr(0x20B9)); print "\n$rupee"."19264/-\n";

How to list the available functions from a module.

BEGIN { no strict 'refs'; print Dumper(\%{"main::YAML::XS::"}); exit; };

Dynamically load module.

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:
my $module = 'My::Module'; eval { (my $file = $module) =~ s|::|/|g; require $file . '.pm'; $module->import(); 1; } or do { my $error = $@; # ... };

Index Implementation.

Single Match:
sub match_positions { my ($regex, $string) = @_; return if not $string =~ /$regex/; return ($-[0], $+[0]); }
Multiple Match:
sub match_all_positions { my ($regex, $string) = @_; my @ret; while ($string =~ /$regex/g) { push @ret, [ $-[0], $+[0] ]; } return @ret }
Linux Stuff: 1.How to kill all processes run by a user on Linux ? -ef | grep vinoth | awk '{ print $2 }' | sudo xargs kill -9
B.pgrep -u vinoth | sudo xargs kill -9
C.sudo pkill -u vinoth
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-08-09 12:24 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (54 votes). Check out past polls.