Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Programming Scripts Articles
Page 5 of 33
Merging Arrays in Perl
Because an array in Perl is just a comma-separated sequence of values, you can combine them together as shown below −Example#!/usr/bin/perl @numbers = (1,3,(4,5,6)); print "numbers = @numbers";OutputThis will produce the following result −numbers = 1 3 4 5 6The embedded arrays just become a part of the main array as shown below −Example#!/usr/bin/perl @odd = (1,3,5); @even = (2, 4, 6); @numbers = (@odd, @even); print "numbers = @numbers";OutputThis will produce the following result −numbers = 1 3 5 2 4 6
Read MoreSelecting Elements from Lists in Perl
The list notation in Perl is identical to that for arrays. You can extract an element from an array by appending square brackets to the list and giving one or more indices −Example#!/usr/bin/perl $var = (5,4,3,2,1)[4]; print "value of var = $var"OutputThis will produce the following result −value of var = 1Similarly, we can extract slices, although without the requirement for a leading @ character −Example#!/usr/bin/perl @list = (5,4,3,2,1)[1..3]; print "Value of list = @list";OutputThis will produce the following result −Value of list = 4 3 2
Read MoreExtracting Keys and Values from Hash in Perl
You can get a list of all of the keys from a hash in Perl by using keys function, which has the following syntax −keys %HASHThis function returns an array of all the keys of the named hash. Following is the example −Example#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @names = keys %data; print "$names[0]"; print "$names[1]"; print "$names[2]";OutputThis will produce the following result −Lisa John Paul KumarSimilarly, you can use values function to get a list of all the values. This function has the following syntax −Syntaxvalues %HASHThis function returns a normal array ...
Read MoreChecking for Key/Value Existence in Perl Hash
If you try to access a key/value pair from a hash in Perl that doesn't exist, you'll normally get the undefined value, and if you have warnings switched on, then you'll get a warning generated at run time. You can get around this by using the exists function, which returns true if the named key exists, irrespective of what its value might be −Example#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); if( exists($data{'Lisa'} ) ) { print "Lisa is $data{'Lisa'} years old"; } else { print "I don't know age of Lisa"; }Here ...
Read MoreGetting Hash Size in Perl
You can get the size - that is, the number of elements from a hash in Perl by using the scalar context on either keys or values. Simply saying first you have to get an array of either the keys or values and then you can get the size of the array as follows −Example#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @keys = keys %data; $size = @keys; print "1 - Hash size: is $size"; @values = values %data; $size = @values; print "2 - Hash size: is $size";OutputThis will produce the following result −1 - Hash size: is 3 2 - Hash size: is 3
Read MoreAdd and Remove Elements in Perl Hashes
Adding a new key/value pair in a Perl hash can be done with one line of code using a simple assignment operator. But to remove an element from the hash you need to use delete function as shown below in the example −Example#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @keys = keys %data; $size = @keys; print "1 - Hash size: is $size"; # adding an element to the hash; $data{'Ali'} = 55; @keys = keys %data; $size = @keys; print "2 - Hash size: is $size"; # delete the same element ...
Read MoreThe ? : Operator in Perl
Let's check the conditional operator? : in Perl which can be used to replace if...else statements. It has the following general form −SyntaxExp1 ? Exp2 : Exp3;Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.The value of a? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. Below is a simple example making use of this operator −Example#!/usr/local/bin/perl $name = "Ali"; $age ...
Read MoreCurrent Date and Time in Perl
Let's start with localtime() function in Perl, which returns values for the current date and time if given no arguments. Following is the 9-element list returned by the localtime function while using in list context −sec, # seconds of minutes from 0 to 61 min, # minutes of hour from 0 to 59 hour, # hours of day from 0 to 24 mday, # day of month from 1 to 31 mon, # month of year from 0 to 11 year, # year since 1900 wday, # days since sunday yday, # days since January 1st isdst # hours of ...
Read MoreGMT Time in Perl
The function gmtime() in Perl works just like localtime() function but the returned values are localized for the standard Greenwich time zone. When called in list context, $isdst, the last value returned by gmtime, is always 0. There is no Daylight Saving Time in GMT.You should make a note on the fact that localtime() will return the current local time on the machine that runs the script and gmtime() will return the universal Greenwich Mean Time, or GMT (or UTC).Try the following example to print the current date and time but on GMT scale −Example#!/usr/local/bin/perl $datestring = gmtime(); print "GMT ...
Read MoreEpoch time in Perl
You can use the time() function in Perl to get epoch time, i.e., the numbers of seconds that have elapsed since a given date, in Unix is January 1, 1970.Example#!/usr/local/bin/perl $epoc = time(); print "Number of seconds since Jan 1, 1970 - $epoc";OutputWhen the above code is executed, it produces the following result −Number of seconds since Jan 1, 1970 - 1361022130You can convert a given number of seconds into date and time string as follows −Example#!/usr/local/bin/perl $datestring = localtime(); print "Current date and time $datestring"; $epoc = time(); $epoc = $epoc - 24 * 60 * 60; # ...
Read More