-
Notifications
You must be signed in to change notification settings - Fork 0
/
mth_to_last_element.pl
38 lines (34 loc) · 1.01 KB
/
mth_to_last_element.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
###############################################################
# script name : mth_to_last_element.pl
# description :
# author : Dai Nguyen-Van
# email : dainv1989@gmail.com
# creation date : 16/10/2013
###############################################################
#!/usr/bin/perl
# open input file
# read content
# remove EOL character
open (DATA, "<", $ARGV[0]) or die $!;
@content = <DATA>;
chomp(@content);
foreach (@content)
{
# check empty line
if ($_ ne "")
{
my @chars = ();
my $index = $_;
$index =~ s/\s//g; # remove all whitespace characters
$index =~ s/[a-zA-Z]//g; # remove all alphabetical characters
$index = $index - 1; # convert position base-1 to base-0
$_ =~ s/\d//g; # remove all digits in line
@chars = split(' ', $_);
if ($index <= $#chars)
{
# print out character at position stored by index
print "$chars[$#chars - $index]\n";
}
}
}
close(DATA);