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