DataManipulation.pm
5.06 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl
#
# Filename:
# ---------
# DataManipulation.pl
#
# Description:
# ------------
# Functions for data manipulation.
#
# Auther:
# -------
# Shinn Lin
#
# Note:
# -----
# none.
#
# Log:
# -----
# 2007/05/04 Create.
#
#BEGIN { push @INC, 'U:\\00MyPerlLib'} # add additional library path
package DataManipulation; # declare package library
use strict;
#******************************************************************************
# Global Data
#******************************************************************************
#******************************************************************************
# Export Function
#******************************************************************************
return 1; # return true
#******************************************************************************
# FUNCTION
# clone
# DESCRIPTION
# clone data from source to destination
# PARAMETERS
# para 1 - reference to source data
# para 2 - reference to destination data
# RETURNS
# none
#******************************************************************************
sub clone()
{
my $src_ref;
my $dest_ref;
($src_ref, $dest_ref) = @_;
# check if types of src and dest are the same
die "can't clone different type [".ref($src_ref)."][".ref($dest_ref)."]\n" if (ref($src_ref) ne ref($dest_ref));
# clone reference-to-hash
if (ref($src_ref) eq "HASH")
{
%{$dest_ref} = ();
while (my ($key, $value) = (each %{$src_ref}))
{
if(ref($value) eq "HASH")
{
%{${$dest_ref}{$key}} = ();
clone( $value, \%{${$dest_ref}{$key}});
}
elsif (ref($value) eq "ARRAY")
{
@{${$dest_ref}{$key}} = ();
clone( $value, \@{${$dest_ref}{$key}});
}
else
{
${$dest_ref}{$key} = $value;
}
}
}
# clone reference-to-array
elsif (ref($src_ref) eq "ARRAY")
{
@{$dest_ref} = ();
for (my $i=0; $i<scalar(@{$src_ref}); $i++)
{
if(ref(${$src_ref}[$i]) eq "HASH")
{
%{${$dest_ref}[$i]} = ();
clone( ${$src_ref}[$i], \%{${$dest_ref}[$i]});
}
elsif (ref(${$src_ref}[$i]) eq "ARRAY")
{
@{${$dest_ref}[$i]} = ();
clone( ${$src_ref}[$i], \@{${$dest_ref}[$i]});
}
else
{
${$dest_ref}[$i] = ${$src_ref}[$i];
}
}
}
else
{
$$src_ref = $$dest_ref;
}
}
sub printObject()
{
my $src_ref;
($src_ref) = @_;
# clone reference-to-hash
if (ref($src_ref) eq "HASH")
{
while(my ($key, $value) = (each %{$src_ref}))
{
print $src_ref."[".$key."] = ".$value."\n";
if((ref($value) eq "HASH") || (ref($value) eq "ARRAY"))
{
&printObject($value);
}
}
}
# clone reference-to-array
elsif(ref($src_ref) eq "ARRAY")
{
for (my $i=0; $i<scalar(@{$src_ref}); $i++)
{
print $src_ref."[".$i."] = ".${$src_ref}[$i]."\n";
if((ref(${$src_ref}[$i]) eq "HASH") || (ref(${$src_ref}[$i]) eq "ARRAY"))
{
&printObject(${$src_ref}[$i]);
}
}
}
else
{
}
}
#******************************************************************************
# Internal Data
#******************************************************************************
#******************************************************************************
# Program Start
#******************************************************************************
#******************************************************************************
# Internal Function
#******************************************************************************
#******************************************************************************
# FUNCTION
# xxx
# DESCRIPTION
# xxx
# PARAMETERS
# xxx
# RETURNS
# xxx
#******************************************************************************
#******************************************************************************
# FUNCTION
# timeCheck
# DESCRIPTION
# print current time (in sec.) and time-difference to previous check if exists
# PARAMETERS
# none
# RETURNS
# current time and time difference if exists (both in sec.)
#******************************************************************************
my $timePrev = 0;
sub timeCheck()
{
my $prePrintStr;
my $postPrintStr;
my $timeCurr = time();
my $timeDiff = 0;
($prePrintStr, $postPrintStr) = @_;
print "$prePrintStr" if ($prePrintStr ne "");
print "[Time: ".$timeCurr." sec.";
if ($timePrev > 0) # previous-time exists
{
$timeDiff = $timeCurr - $timePrev;
print "(Diff = $timeDiff)";
}
print "]";
print "$postPrintStr" if ($postPrintStr ne "");
print "\n\n";
$timePrev = $timeCurr;
return $timeDiff;
}