Programming

How to iterate or loop through all elements of an array in a perl script

Here you can see multiple ways of implementing array iterator.

Example1:

This example reads each element of the array to a variable “$item” in a loop.

@myarray = ('One', 'Two', 'Three');
foreach $item (@myarray) {
  print $item;
}

Example2:

This example avoids intermediate variable “$item” shown in the above script. However if you have nested loops, this method is not suitable. Each element is available in a special variable “$_” inside the loop.

@myarray = ('One', 'Two', 'Three');
foreach (@myarray) {
  print $_;
}

Example3:

This example is similar to “for” loop style of a C programmer .

@myarray = ('One', 'Two', 'Three');
for($i=0; $i <= $#myarray; More >

How to pass and read function or subroutine arguments in a perl script

All the arguments passed to a perl subroutine are packed in an array “@_”. The following script shows you how to access function arguments in a subroutine.

[nick@techpulp ~]# cat funargs.pl
#!/usr/bin/perl

sub myfun
{
  my $i = 0;
  $nargs = $#_+1;
  print "Number of arguments: $nargs\n";
  foreach $arg (@_) {
    $i++;
    print "Arg $i: $arg\n";
  }
}

myfun("One", "Two");
[nick@techpulp ~]#

Let us run the above script.

[nick@techpulp ~]# ./funargs.pl
Number of arguments: 2
Arg 1: One
Arg 2: Two
[nick@techpulp ~]#

If the subroutine is expected to take fixed number of arguments, you can simplify the script as following.

[nick@techpulp ~]# More >

How to read input line by line in a perl script

The following perl script reads one line at a time from the standard input which can be from normal console or a redirected file using pipes.

[nick@techpulp ~]# cat readline.pl
#!/usr/bin/perl

$line = 0;
while (<>) {
  $line++;
  print "Line $line: " . $_;
}
[nick@techpulp ~]# chmod +x readline.pl
[nick@techpulp ~]#

In the above script, The “<>” reads a line from standard input file of the script and “$_” contains the last read data.

For input, let us consider following file (input.txt) which contains four lines.

[nick@techpulp ~]# cat input.txt
first line
second line
third line
fourth line
[nick@techpulp ~]#

Now let More >

How to read or access command line arguments in a perl script

All command line arguments passed to a perl script are stored in a array named @ARGV. So $ARGV[0] contains first argument, $ARGV[1] contains second argument and so on. As all command line arguments are packed in an array, you can use $#ARGV to get the index of last element. That means ($#ARGV+1) will give you number command line arguments passed to the script.

Look at the following example, that iterates through all command line arguments passed.

[nick@techpulp ~]# cat cmline.pl
#!/usr/bin/perl

$nargs = $#ARGV + 1;
print "you have passed $nargs command line arguments\n";

for($i=0;$i<$nargs;$i++) {
   print "Arg $i: $ARGV[$i]\n";
}
[nick@techpulp More >

How to include a CSS stylesheet in another style sheet – Use import url statement

You can use “import url(..)” statement to include a CSS file with in an another. For example, you can include another css file (file2.css) in a style sheet file file1.css by placing following statement in file1.css.

@import url(file2.css);

The above url is relative and file2.css is expected to be available at the same directory level as file1.css.

Otherwise you can specify a complete URL or a relative URL as shown in the following examples.

Import from a directory with absolute path on the same server:

@import url(/css/file2.css);

Import from a directory with relative path on the same server:

@import url(../css2/file2.css);

Import from another server using More >

How to set various colors for A links using CSS

The CSS supports following for <A> tag.

Name Description a:link Style is applied to all non-visited links a:visited Style is applied to all visited links a:hover Style is applied when mouse moves over a link a:active Style is applied to active links. A link becomes active when it is clicked

It is always recommended to specify the styles for a link in the same order as specified in the above table.

An example style specification:

<style type="text/css">
a:link {
  color: #ff0000;
  background-color: #00ffff;
  text-decoration: none
}

a:visited {
  color: #00ff00;
  background-color: #ff00ff;
  text-decoration: none
}

a:active {
  color: #0000ff;
  background-color: #ffff00;
  text-decoration: none More >

How to add a new user to MySQL and set privileges

First you need to connect to MySQL server with a user who has administrative priveleges.

[root@techpulp ~]# mysql -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.67 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Let us assume you are creating an user named “neo” with password “neopass“and you would like to grant all privileges on the database “articles“.

mysql> GRANT ALL ON articles.* to 'neo'@'localhost' identified by 'neopass' WITH GRANT OPTION;

After adding a new user, you need More >

How to create rounded border for DIV, PRE like HTML elements

The following CSS code enables rounded borders for elements like DIV, PRE etc. However it may not work in Internet Explorer (IE) as it is specific to certain browsers like Firefox.

pre {

-moz-border-radius: 3px;
-khtml-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;

}

The above CSS defines 3px radius for the rounded element. You can change it to a value of your choice.

How to define scrollable and fixed width DIV or PRE tag in HTML using CSS

With fixed width websites, it is often desired to fix the maximum width of certain elements like <DIV>, <PRE> etc. However by fixing the maximum width of these elements, the content inside them may either disappear or may interfere with the content of element that appear near to them.

CSS supports an attribute “overflow” which can be used to define the way how browser should behave if the contents of such element tend to go beyond width or height of the element. The following are the possible values for “overflow” attribute.

Value Meaning auto Display horizontal or vertical scroll bar automatically when More >