Category Archives: Uncategorized

How to remove newlines or line separators in perl?

Using the Perl chomp() function

Introduction

The chomp() function will remove (usually) any newline character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline.

For more information on the $/ variable, try perldoc perlvar and see the entry for $/.

Example 1. Chomping a string

Most often you will use chomp() when reading data from a file or from a user. When reading user input from the standard input stream (STDIN) for instance, you get a newline character with each line of data. chomp() is really useful in this case because you do not need to write a regular expression and you do not need to worry about it removing needed characters.

When running the example below, using the enter key on a line by itself will exit the program.

  #!/usr/bin/perl
  use strict;
  use warnings;

  while (my $text = <STDIN>) {
    chomp($text);
    print "You entered '$text'\n";
    last if ($text eq '');
  }

Example usage and output of this program is:

  a word
  You entered 'a word'
  some text
  You entered 'some text'

  You entered ''

Example 2. Chomping an array

If you chomp an array, it will remove a newline from the end of every element in the array:

  #!/usr/bin/perl
  use strict;
  use warnings;

  my @array = ("bob\n", "jill", "fred\n");

  print "Before chomp:\n";
  print "@array\n";

  chomp(@array);

  print "After chomp:\n";
  print "@array\n";

This program produces the following output:

  Before chomp:
  bob
   jill fred

  After chomp:
  bob jill fred

As you can see, the newlines have been removed from “bob” and “fred”, but no characters have been removed from “jill”.

Example 3. Chomping a hash

If you pass a hash into chomp(), it will remove newlines from every value (not key) of the hash:

  #!/usr/bin/perl
  use strict;
  use warnings;

  my %hash = (
    'first' => "one\n",
    'second' => "two\n",
    'third' => "three\n",
  );

  chomp(%hash);

  foreach my $k (keys %hash) {
    print "$k: $hash{$k} ";
  }

  print "\n";

  exit 0;

This program outputs:

  first: one second: two third: three

How to remove last character from a string in perl?

Using the Perl chop() function

Introduction

Sometimes you will find you want to unconditionally remove the last character from a string. While you can easily do this with regular expressions, chop is more efficient.

The chop() function will remove the last character of a string (or group of strings) regardless of what that character is. Note, if you want to easily remove newlines or line separators you have to use the chomp() .

Example 1. Chopping a string

The chop() function removes and returns the last character from the given string:

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $string = 'frog';

  my $chr = chop($string);

  print "String: $string\n";
  print "Char: $chr\n";

This program gives you:

  String: fro
  Char: g

If the string is empty, chop() will return an empty string. If the string is undefined, chop() will return undefined.

Example 2. Chopping strings in an array

If you pass the chop() function an array, it will remove the last character from every element in the array.

Note that this will only work for a one-dimensional array. In other words, it is not valid to pass in an array reference, or an array that contains an array (or hash).

  #!/usr/bin/perl
  use strict;
  use warnings;

  my @array = ('fred', 'bob', 'jill', 'joan');

  my $chr = chop(@array);

  foreach my $str (@array) {
    print "$str\n";
  }

  print "Char: $chr\n";

This produces the output:

  fre
  bo
  jil
  joa
  Char: n

Example 3. Chopping strings in a hash

If you pass a hash into chop(), it will remove the last character from the values (not the keys) in the hash. For example:

  #!/usr/bin/perl
  use strict;
  use warnings;

  my %hash = (
    first => 'one',
    second => 'two',
    third => 'three',
  );

  my $chr = chop(%hash);

  foreach my $k (keys %hash) {
    print "$k: $hash{$k}\n";
  }

  print "Char: $chr\n";

This program outputs:

  first: on
  second: tw
  third: thre
  Char: e

Note that as with arrays, chop is not designed to process hash reference or hashes containing other hashes (or arrays).

Today’s Date in Perl in MM/DD/YYYY format

It can be done easily as :

use POSIX qw(strftime);

my $date = strftime "%m/%d/%Y", localtime;
print $date;

UPDATE multiple tables in single query?

It’s as follows :-

UPDATE table1 a INNER JOIN table2 b ON a.task_id = b.task_id SET a.column='val' b.column=UNIX_TIMESTAMP(CONVERT_TZ(CURRENT_TIMESTAMP,'+05:30','+00:00')) WHERE a.task_id='1024' AND b.task_id='1024'

INSERT a data from a table to another in a single query…

It’s as follows…

INSERT INTO table2 SELECT * FROM table1;

or to copy a few columns

INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

They can also be written as follows

SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;

or to copy a few columns

SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;