Skip to main content

03-perl Numbers and Strings



Numbers in perl are pretty straight forward.it simply declares with variables as we talked before.For example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/perl
 
use strict;
use warnings;
 
main(@ARGV);
 
sub main
{
      my $variable1 = 1234;
      my $variable2 = 01234;
      my $variable3 = 0x1ab7;
      my $variable4 = 0b10101010;
      my $variable5 = 12.345;
      my $variable6 = 12.4e34;
      
      print("$variable1\n");
      print("$variable2\n");
      print("$variable3\n");
      print("$variable4\n");
      print("$variable5\n");
      print("$variable6\n");      
}


In the above example variable1 will print 1234.Variable2 will print 668 because if a number with leading zero, perl will take it as octal and when printing convert in to decimal that gives you 668.Variable3 will print 6839 perl will take the value as hexadecimal because with a leading 0x and convert to decimal to base ten when printing.Variable4 will print 170 and 0b recognize as binary,perl will take it as binary and covert decimal ten.Variable5 will print a floating number.Variable6 will print exponential.

we can do any arithmetic operation on perl.using basic arithmetic operators like + – * / .

Lets talk about perl string.

string in perl represented by arguments inside ” ” and single quote ‘ ‘. for example “Hello world” this will considered as single string and as in single quotes too. For example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/perl
 
use strict;
use warnings;
 
main(@ARGV);
 
sub main
{
      my $variable1 = "Hello";
      my $variable2 = 'World';
      print($variable1);
      print($variable2);
}


String interpolation

String interpolation helps you to pass argument in side a string variable.Lets go though with an example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/perl
 
use strict;
use warnings;
 
main(@ARGV);
 
sub main
{
      my $variable1 = "Hello";
      print("$variable World\n");
}


This will print Hello World . value “Hello world” will pass inside the print statement.

This will work with only in double quotes not with single quote because single quote will not analyse the input and simply print, but double quote will analyse.

There is another way to do ” “ and ‘ ‘ using q and qq. q represents single quote and qq represents double quotes.lets look in to example how to implement it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/perl
 
use strict;
use warnings;
 
main(@ARGV);
 
sub main
{     my $n         = "World";
      my $variable1 = q{Hello $n};
      my $variable2 = qq{Hello $n};
      print($variable1."\n");
      print($variable2."\n");
}


as i said q represents single quotes,the arguments can be placed in between any delemeter here its {} or you can use || leading with q in-front.In the above example variable1 will print Hello $n because single quote will not analyze the string argument.

qq represents double quotes,as said above we can implement with delemeters and variable2 will print Hello World. Because double quotes will analyze string argument.

string concatenation

string concatenation or join two strings or many by using dot “.” .We can concate any string dot but “+” cannot be use in perl for concatenation its strictly used for numerical operation .let look in to an example how to concatenate.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/perl
 
use strict;
use warnings;
 
main(@ARGV);
 
sub main
{
      my $variable1 = "Hello";
      my $variable2 = 'World';
      print($variable1." ".$variable2."\n");
}

Comments

Popular posts from this blog

creating first go program

for creating go program open your editor and paste this code, and save it as filename.go for example helloworld.go

package main import "fmt" func main() { fmt.Println("Hello, 世界") }
I will explain these codes in details for now , we will look how to run these code.

to run type go run helloworld.go  in terminal , we get


you can try golang online as well , golang