Perl index() function

Perl index() function

The index() function is used to identify or search for the  position of a letter or a substring in a string. It will return the position value of the first occurance of the substring within the string.

$index = index(‘iguddy’, ‘gud’);

print $index . “n”;

This will print 1, which is the index value of the start of the substring gud

The index() function is similar to a regular expression call, but without all the overhead of the regex search patterns. It simply returns the index value of the substring if it exists within the string.

Example 1:

Let’s consider you are searching for a particular letter ‘u’ in the following string “iguddy.com”. The following code snippet will help you find the letter ‘u’ in the String “IGuddy.com”

 #!/usr/bin/perl

my $string = ‘iguddy.com’;

my $letter = ‘u’;

my $result = index($string, $letter);

print “Result: $resultn”;

This program gives you:

  Result: 2

Example 2:

The index() function will return -1 if the substring is not present in the actual string.For example, we can look for the letter “A” in the string “iguddy.com”:

 #!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘A’;

my $result = index($string, $char);

print “Result: $resultn”;

The program outputs:

  Result: -1

Example 3:

If the letter we’re searching for appears more than once in the string, index() return the index of the first occurrence of the letter.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $result = index($string, $char);

print “Result: $resultn”;

This program gives you:

Result: 3

Example 4 :

Instead of searching for a character we can also search for a substring using index() function.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $substr = ‘gud’;

my $result = index($string, $substr);

print “Result: $resultn”;

This program gives you:

Result: 1

Example 5:

The index() function always return the index of first occurrence of the letter. We can also look for the second occurrence as below,

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $offset = 2;

my $result = index($string, $char, $offset);

print “Result: $resultn”;

The program outputs:

Result: 4

Example 6:

Index() in a loop to find out all the occurrence of the substring.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $offset = 0;

my $result = index($string, $char, $offset);

while ($result != -1) {

print “Found $char at $resultn”;

$offset = $result + 1;

$result = index($string, $char, $offset);

}

The above will gives the following output

Found e at 3

Found e at 4

Example 7:

To find the last occurrence of the substring using index() frunction.

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $char = ‘d’;

my $result = rindex($string, $char);

print “Result: $resultn”;

This would produce:

Result: 4

Example 8:

Is there any way of ignoring case in Perl’s index function?  The Answer is “Yes” there is a way. Just find below the code snippet,

#!/usr/bin/perl

my $string = ‘iguddy.com’;

my $substr = ‘GUD’;

if (index lc($string),lc($substr) > -1 {

print “Found : Result: $resultn”;

}else{

print “Not Found : Result: $resultn”;

}

This program gives you:

Result: 1

Relational and Logical Operators in PERL

Relational and Logical Operators in PERL

All programming languages have Relational and logical operators. Lets discuss on what Perl has as relational & logical operators.

  • The relational & logical operators are used with conditional statements and looping statements.
  • Perl’s relational and logical operators are same as C language and shell script.
  • All operators’ returns true if the condition satisfies else return False.

Relational operators

The C way

The shell script way

Description

>

gt

Greater than

<

lt

Less than

>=

ge

Greater than equal to

<=

le

Less than equal to

==

eq

Equals

!=

ne

Not equal

Logical operator

The C way

Description

&&

and operator

||

or operator

!

not operator

Other file operations

Operators

Description

-e filename File exist
-f filename Plane file
-d filename Directory
-s filename File size not zero
-r filename File readable
-w filename File writable
-x filename File executable

Introduction to Perl

Introduction to Perl

Perl is a interpreted language. It is almost available in all popular operating systems today. Perl will be similar to C language and awk.

Basics

  • Perl script file mostly will have the extension .pl
  • In unix and Linux flavours, the perl file should have an execution permission.
  • Start the perl script file with #!/usr/bin/perl
  • Dont forget to terminate perl commands with ; (semicolen). eg., print “Hello worldn”;

Program 1

Always the first program I try on any language is hello world, so perl’s hello world program is here for Linux and UNIX flavors,

#!/usr/bin/perl

print “Hello world.n”;

In Windows the same program will look like,

#!D:/perl/perl.exe

print “Hello world.n”;

  • The first line (in both examples) is a comment for perl, but for shell it tells which interpriter in what path to use on the file.
  • The second command print is similar to printf in C language with some difference. Let’s learn the difference later.

Syntax to run Perl programs

perl <program name> <arguments>

PERL Data Types and Objects

PERL Data Types and Objects

Perl has three data types:

  • scalars – Scalars can hold elementary data, i.e., string, integer, float …
  • arrays of scalars – Arrays of scalars in short arrays can hold arrays of scalar values.
  • associative arrays of scalars. – Associative arrays of scalars are also called as hashes contain key, value pairs. There are like arrays only but the difference is these are indexed by strings where arrays are indexed by integers.

Declaration of scalar

Syntax :   $var_name
Example: $name   # holds string value
$num    #holds numeric value

Note: no differentiation in declaring string or numeric variable.

Declaration of Arrays    

Syntax : @arr_name
Example: @name  #holds a list of scalar values

Declaration of Hashes

Syntax : %hash_name

Accessing Scalar:

$var_name

Accessing Arrays:

Single element at index ‘i’ – $arr_name[i]
All elements at once –  @arr_name

Accessing Hashes:

Single element at ‘key’ position –  $hash_name(key)
All elements at once – %hash_name

 

If You need more visit our site – www.iguddy.com