Programming Scripts Articles

Page 5 of 33

Merging Arrays in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 2K+ Views

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 More

Selecting Elements from Lists in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 574 Views

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 More

Extracting Keys and Values from Hash in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 3K+ Views

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 More

Checking for Key/Value Existence in Perl Hash

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 661 Views

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 More

Getting Hash Size in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 3K+ Views

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 More

Add and Remove Elements in Perl Hashes

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 959 Views

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 More

The ? : Operator in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 532 Views

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 More

Current Date and Time in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 604 Views

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 More

GMT Time in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 896 Views

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 More

Epoch time in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 9K+ Views

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
Showing 41–50 of 328 articles
« Prev 1 3 4 5 6 7 33 Next »
Advertisements