-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap_elements.pl
56 lines (50 loc) · 1.68 KB
/
swap_elements.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
####################################################
# script name : swap_elements.pl
# description : You are given a list of numbers which is supplemented with positions that have to be swapped.
# author : Dai Nguyen-Van
# email : dainv1989@gmail.com
# creation date : 16/10/2013
####################################################
#
#!/usr/bin/perl
# open file in read-only mode
# read content of input file
# remove all end of line character (EOL)
open (DATA, "<", $ARGV[0]) or die $!;
@content = <DATA>;
chomp(@content);
foreach (@content)
{
# if line is not empty
if ($_ ne "")
{
# find the position of colon character
$idx = index($_, ":");
# if colon character is exist
if ($idx != -1)
{
# get array of numbers need to swap elements
$array_str = substr($_, 0, $idx - 1);
@array_num = split(" ", $array_str);
# get swap positions
$swap_str = substr($_, $idx + 1, length($_) - $idx - 1);
@swap_arr_str = split(",", $swap_str);
foreach $item(@swap_arr_str)
{
# remove all space character
# get positions need to swap value
$item =~ s/\s//g;
@position = split("-", $item);
# swap value using mediate variable
my $mediate = $array_num[$position[0]];
$array_num[$position[0]] = $array_num[$position[1]];
$array_num[$position[1]] = $mediate;
}
# print out array of number after swap
print join " ",@array_num;
print "\n";
}
}
}
# close file handle
close(DATA);