Guys,
Please explain what is happening here?

#!/usr/bin/perl
$line="abcd";
@arr=split("",$line);
print $arr[$0];
$x="x";
if ($arr[0]==$x)
{
print "Hello";
}

The output is:
aHello

Am I dreaming this?

#!/usr/bin/perl

#declare a variable named line and assign value 'abcd' to it
$line="abcd";

#split the variable line ("abcd") into 1 char part
@arr=split("",$line);

#show/print the 1st character, which is 'a' 
print $arr[$0];

#declare a variable named line and assign value 'x' to it
$x="x";

#if the 1st character for $line is equal to $x, show/print "Hello"
if ($arr[0] eq $x)
{
print "Hello";
}

Based on your coding, the output should be 'a' only. Unlesss....... In your world, 'a' is equal to 'x'. :D

Edited 6 Years Ago by raul15791: typo

Hmm...actually, something here strikes me as odd.

print $arr[$0];

$0 is a special variable in Perl equal to the name of the source file for the program running. I just created the script below, named the file "2", and ran it:

#!/usr/bin/perl -w
@arr = ('a','b','c','d');
print $arr[$0],"\n";
exit;

My output was "c". :) Gotta watch those built-in variables.

If the OP had enabled warnings, and named the script something other than a number, then the following warning would have been helpful:
Argument "/home/david/Programming/Perl/weird.pl" isn't numeric in array element at /home/david/Programming/Perl/weird.pl line [whatever].

This question has already been answered. Start a new discussion instead.