Programming Scripts Articles

Page 7 of 33

References to Functions in Perl

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

While using Perl script, this might happen if you need to create a signal handler so you can produce a reference to a function by preceding that function name with \& and to dereference that reference you simply need to prefix reference variable using ampersand &. Following is an example −Example#!/usr/bin/perl # Function definition sub PrintHash {    my (%hash) = @_;    foreach $item (%hash) {       print "Item : $item";    } } %hash = ('name' => 'Tom', 'age' => 19); # Create a reference to above function. $cref = \&PrintHash; # Function call using reference. ...

Read More

How to use Formats in Perl?

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

In order to invoke a format declaration in Perl Script, we would use the write keyword −write EMPLOYEE;The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle. As we want the data sent to the STDOUT, we must associate EMPLOYEE with the STDOUT filehandle. First, however, we must make sure that that STDOUT is our selected file handle, using the select() function.select(STDOUT);We would then associate EMPLOYEE with STDOUT by setting the new format name with STDOUT, using the special variable $~ or $FORMAT_NAME as ...

Read More

Create a Report Header using Perl

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

Sometime you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this using Perl. Apart from defining a template you would have to define a header and assign it to $^ or $FORMAT_TOP_NAME variable −Example#!/usr/bin/perl format EMPLOYEE = =================================== @

Read More

Swap Upper diagonal with Lower in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 387 Views

This tutorial is designed to swap the upper row of a three-diagonal array to its lower one using c++ code. Moreover, if a three-diagonal array is an input, the coveted results must be something like that as;For this, the course of action is briefed in the algorithm as follows;AlgorithmStep-1: Input a diagonal array Step-2: Pass it to Swap() method Step-3: Traverse the outer loop till 3 Step-4: increment j= i+ 1 in the inner loop till 3 Step-5: put the array value in a temp variable Step-6: interchange the value arr[i][j]= arr[j][i] Step-7: put the temp data to arr[j][i] Step-8: ...

Read More

The Match Operator in Perl

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

The match operator m// in Perl, is used to match a string or statement to a regular expression. For example, to match the character sequence "foo" against the scalar $bar, you might use a statement like this −Example#!/usr/bin/perl $bar = "This is foo and again foo"; if ($bar =~ /foo/) {    print "First time is matching";    } else {    print "First time is not matching"; } $bar = "foo"; if ($bar =~ /foo/) {    print "Second time is matching";    } else {    print "Second time is not matching"; }When above program is executed, it ...

Read More

Matching Only Once in Perl

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

There is a simpler version of the match operator in Perl - the ?PATTERN? operator. This is basically identical to the m// operator except that it only matches once within the string you are searching between each call to reset.For example, you can use this to get the first and last elements within a list −Example#!/usr/bin/perl @list = qw/food foosball subeo footnote terfoot canic footbrdige/; foreach (@list) {    $first = $1 if /(foo.*?)/;    $last = $1 if /(foo.*)/; } print "First: $first, Last: $last";When the above program is executed, it produces the following result −First: foo, Last: footbrdige

Read More

The Substitution Operator in Perl

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

The substitution operator s/// in Perl is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is −s/PATTERN/REPLACEMENT/;The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with. For example, we can replace all occurrences of dog with cat using the following regular expression −Example#/user/bin/perl $string = "The cat sat on the mat"; $string =~ s/cat/dog/; print "$string";When the above program is ...

Read More

Grouping Matching in Perl

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

From a regular-expression point of view in Perl, there is no difference between the following two expressions except that the former is slightly clearer.$string =~ /(\S+)\s+(\S+)/; and $string =~ /\S+\s+\S+/;However, the benefit of grouping is that it allows us to extract a sequence from a regular expression. Groupings are returned as a list in the order in which they appear in the original. For example, in the following fragment we have pulled out the hours, minutes, and seconds from a string.my ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/);As well as this direct method, matched groups are also available within the ...

Read More

The G Assertion in Perl

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

The \G assertion in Perl allows you to continue searching from the point where the last match occurred. For example, in the following code, we have used \G so that we can search to the correct position and then extract some information, without having to create a more complex, single regular expression −Example#!/usr/bin/perl $string = "The time is: 12:31:02 on 4/12/00"; $string =~ /:\s+/g; ($time) = ($string =~ /\G(\d+:\d+:\d+)/); $string =~ /.+\s+/g; ($date) = ($string =~ m{\G(\d+/\d+/\d+)}); print "Time: $time, Date: $date";When the above program is executed, it produces the following result −Time: 12:31:02, Date: 4/12/00The \G assertion is actually ...

Read More

What are Packages in Perl?

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

The package statement in Perl switches the current naming context to a specified namespace (symbol table). Thus −A package is a collection of code which lives in its own namespace.A namespace is a named collection of unique variable names (also called a symbol table).Namespaces prevent variable name collisions between packages.Packages enable the construction of modules which, when used, won't clobber variables and functions outside of the modules's own namespace.The package stays in effect until either another package statement is invoked, or until the end of the current block or file.You can explicitly refer to variables within a package using the :: package ...

Read More
Showing 61–70 of 328 articles
« Prev 1 5 6 7 8 9 33 Next »
Advertisements