Saturday, December 25, 2010

Chomp

Method 1:

#!/usr/local/bin/perl
use strict;
use warnings;
chomp(my @colors = <DATA>);
print "\@colors = @colors\n";


__DATA__
red
yellow
White




Method 2:


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

# scalar example
my $nr = chomp (my $color = "blue\n");
print "Newlines removed: $nr\n";
# it expects: Newlines removed: 1

# array example
my @array = ("blue\n", 1, 3.14, "12\n", "Perl");
$nr = chomp(@array);
print "Newlines removed: $nr\n";
# it expects: Newlines removed: 2

# hash example
my %ages = ("John\n", 45, "Paul\n", "25\n", "Marie\n", "22\n");
$nr = chomp %ages;
# it removes only the newlines from the hash values!
print "Newlines removed: $nr\n";
# it expects: Newlines removed: 2





Method 3:


#!/usr/local/bin/perl

use strict;
use warnings;

my @array = ();
foreach my $line (<DATA>) {
    $line =~ s/\r?\n$/*;*/
    push @array, $line;
}

# print the array
foreach (@array) {
    print "$_\n";
}

__DATA__
first line
second line




Method 4:


#!/usr/local/bin/perl

use strict;
use warnings;

# initialize an anonymous array and define a reference to it
my $arrayRef = ["John\n", "Peter", "Alice\n"];

# chomp the array reference
my $nr = chomp @$arrayRef;
print "Characters removed: $nr\n";
print "The array content: @$arrayRef\n";



Method 5:



use strict;
use warnings;

while(defined(my $input = <STDIN>)) {
       chomp $input;
        print "input line: $input\n";
}


Method 6:


#!/usr/local/bin/perl

use strict;
use warnings;

# initialize a hash
my %ages = ("John\n", "43\n", "Paul\n", "25\n", "Marie\n","22\n");
my $nr = chomp %ages;
print "Newlines removed: $nr\n";

# well, I use Data::Dumper module to see
# what it is in the hash
use Data::Dumper;
print Dumper(%ages);    #only the hash values will be chomped



Method 7:


#!/usr/local/bin/perl

  use strict;
  use warnings;

# initialize a hash
my %fruits = map {chomp; $_ => 0} <DATA>;

# I’ll print the hash keys using the foreach statement
# as a modifier of a statement
print "$_ " foreach (keys %fruits);
print "\n";

__DATA__
apricot
cherry
plum


Method 8:



#!/usr/local/bin/perl

use strict;
use warnings;

# initialize an anonymous hash and defined a reference to it
my $hashRef = {"John\n", "23", "Peter", "45\n", "Alice\n", "32"};

# chomp the hash reference
my $nr = chomp %$hashRef;
print "Characters removed: $nr\n";


Method 9:


#!/usr/local/bin/perl

use strict;
use warnings;
use Tie::IxHash;

# read the file one record at the time
  my % fruitsColors = ();
  while(<DATA>) {
     chomp;
     my @line = split(/,/);
     $fruitsColors{$line[0]} = $line[1]; }

use Data::Dumper;
print Dumper(%fruitsColors);

__DATA__
apricot,yellow
cherry,red
plum,darkblue


Method 10:


#!/usr/local/bin/perl

use strict;
use warnings;
open (FN, "cities.txt") || die "Can't open cities.txt: $!";
my @cities;
while (<FN>) {
    chomp;
    next if index(uc $_, "EUROPE") == -1;
    # or next if index(lc $_, "europe") == -1;
    my @tmp = split(/:/);
    push @cities, $tmp[2];
    # or unshift @cities, $tmp[2]; } print "Cities from Europe: @cities\n";





Method 11:

use strict;
use warnings;

chomp (my $choice = lc <STDIN>);
if ($choice eq 'yes') {
    print "Choice is Yes\n";
}
elsif ($choice eq 'no') {
      print "Choice is No\n";
} else {
      print "Error: Choice $choice\n";
}



Method 12:

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

# initialize the array
my @fruits = ("lemon\n", "orange\n", "plum\n", "nut\n");
my $nr;

map {
       $nr += chomp;
       # some other statements here
} @fruits;


print "Characters removed: $nr\n";
print "The array content: @fruits\n";


Method 13:

#!/usr/local/bin/perl

use strict;
use warnings;

# initialize an anonymous hash and defined a reference to it
my $hashRef = {"John\n", "23", "Peter", "45\n", "Alice\n", "32"};
my $nr;

my %newHash = map {
    # save and chomp the current hash value
    my $val = ${$hashRef}{$_};

  # or my $val = $$hashRef{$_};
  # or my $val = $hashRef->{$_};

  $nr += chomp $val;

  # chomp the key stored in $_
  $nr += chomp;
  # add the new element to %newHash
  $_ => $val; } keys %$hashRef;

  print "Total characters removed: $nr\n";

  # see what is in the new hash
  use Data::Dumper;
  print Dumper(%newHash);



Method 14:



#!/usr/local/bin/perl

use warnings;
use strict;

# initialize an array with an empty list
my @array = ();
{
    local $/ = "";  #paragraph mode
    foreach (<DATA>) {
        chomp;
        push @array, $_;
    }
}

print $_, "\n" foreach (@array);


__DATA__
The if statement is very often used in Perl.

This is Perl.
Some Perl functions here: push, substr, index.


This is Perl too.




Method 15:


#!/usr/local/bin/perl

use strict;
use warnings;

# save the current value of $/
my $crtValue = $/;

$/ = "";
my $v = "\n\nsome text here\n\n\n\n";       #End of newline character only removed.
my $nr = chomp $v;
print "The last $nr newlines have been removed: text is $v\n";




Method 16:



#!/usr/local/bin/perl

use strict;
use warnings;

print "Type a word: ";
my $scalarVar = <STDIN>;

# define a scalar reference
my $scalarVarRef = \$scalarVar;

# chomp the reference
my $nr = chomp $$scalarVarRef;
print "Characters removed: $nr\n";
# it outputs: Characters removed: 1



Method 17:



#!/usr/local/bin/perl

use strict;
use warnings;

foreach my $line (<DATA>) {
    chomp $line;
    my ($lastname, $firstname, $country, $age) = split /,/,$line;
    print "Last Name: $lastname, First Name: $firstname, Country: $country, Age: $age\n";  }

__DATA__
John,Silva,USA,23
Antoine,Chevron,France,45



Method 18:



#!/usr/local/bin/perl

use strict;
use warnings;

print "User name: ";
#my $name = <STDIN>;
my $name = <>;
chomp ($name);
print "$name is your user name\n";



Method 19:



#!/usr/local/bin/perl
#unpact is convert to strings to ascii.

use strict;
use warnings;

{
    local $/ = undef;
    my $str = <DATA>;
    print(unpack('H*', $str), "\n");    #value was printed in hexadecimal characters by using the unpack function
}
# it expects to print: 310a320a330a340a

__DATA__

1
2
3
4



Method 20:

use strict;
use warnings;

# initialize the array
my @fruits = ("lemon\n", "orange\n", "plum\n", "nut\n");
my $nr;
my $count = scalar @fruits;
while ($count--) {
   $nr += chomp $fruits[$count];
    # some other statements here
}
   print "Characters removed: $nr\n";
   print "The array content: @fruits\n";

No comments: