0% found this document useful (0 votes)
5 views

Perl Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Perl Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#!

/usr/bin/perl

# This will print "Hello, World"


print "Hello, world\n";

print("Hello, world\n");
print "Hello, world\n";

# Perl program to demonstrate qw function

# using qw function
@arr1 = qw /This is a Perl Tutorial by GeeksforGeeks/;

# Creates array2 with elements at


# index 2,3,4 in array1
@arr2 = @arr1[2,3,4];

print "Elements of arr1 are:\n";


foreach $ele (@arr1)
{
print "$ele \n";
}

print "Elements of arr2 are:\n";


foreach $ele (@arr2)
{
print "$ele \n";
}

ARRAY OPERATIONS

# Initializing the array


@x = ('Java', 'C', 'C++');

# Print the Initial array


print "Original array: @x \n";

# Pushing multiple values in the array


push(@x, 'Python', 'Perl');

# Printing the array


print "Updated array: @x\n";

print "Value returned by pop: ", pop(@x);

# Prints the array after pop operation


print "\nUpdated array: @x\n";

print "Value returned by shift: ",


shift(@x);

# Array after shift operation


print "\nUpdated array: @x\n";
print "No of elements returned by unshift: ",
unshift(@x, 'PHP', 'JSP');

# Array after unshift operation


print "\nUpdated array: @x\n";

ACCESSING ARRAY ELEMENTS

# Perl program to demonstrate Array's


# creation and accessing the array's elements

# Creation an array fruits


@fruits = ("apple", "banana", "pineapple", "kiwi");

# printing the array


print "@fruits\n";

# Prints the array's elements


# one by one using index
print "$fruits[0]\n";
print "$fruits[1]\n";
print "$fruits[2]\n";
print "$fruits[3]\n";

ARRAY ITERATION

# Perl program to illustrate


# iteration through range

# array creation
@arr = (11, 22, 33, 44, 55);

# size of array
$len = @arr;

for ($b = 0; $b < $len; $b = $b + 1)


{
print "\@arr[$b] = $arr[$b]\n";
}

# Perl program to illustrate Iterating


# through elements(foreach Loop)

# creating array
@l = (11, 22, 33, 44, 55);

# foreach loop
foreach $a (@l)
{
print "$a ";
}

You might also like