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?
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?
You use "eq" when comparing strings, not "==".
#!/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
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.