Programming Scripts Articles

Page 21 of 33

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 682 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 982 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 548 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 622 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 912 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

Define and Call a Subroutine in Perl

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

The general form of a subroutine definition in Perl programming language is as follows −sub subroutine_name {    body of the subroutine }The typical way of calling that Perl subroutine is as follows −subroutine_name( list of arguments );In versions of Perl before 5.0, the syntax for calling subroutines was slightly different as shown below. This still works in the newest versions of Perl, but it is not recommended since it bypasses the subroutine prototypes.&subroutine_name( list of arguments );Let's have a look into the following example, which defines a simple function and then call it. Because Perl compiles your program before ...

Read More

Passing Arguments to a Subroutine in Perl

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

You can pass various arguments to a Perl subroutine like you do in any other programming language and they can be accessed inside the function using the special array @_. Thus the first argument to the function is in $_[0], the second is in $_[1], and so on.You can pass arrays and hashes as arguments like any scalar but passing more than one array or hash normally causes them to lose their separate identities. So we will use references ( explained in the next chapter ) to pass an array or hash.Let's try the following example, which takes a list ...

Read More
Showing 201–210 of 328 articles
« Prev 1 19 20 21 22 23 33 Next »
Advertisements