Dear Monks,
I have to anonymize several files (semicolon-separated).
The following script does the job.
But: if the field "Number_1" is the last field, then it stays unchanged. With the dummy field (see DATA) it works. Where is the mistake?
Thank you very much!
VE
use strict;
use Text::ParseWords;
while(<DATA>) {
my @Proto = <DATA>;
my $Header = shift @Proto;
my @Assembly;
my $Nr = &parsingHeader($Header, "Number");
my $Nr_1 = &parsingHeader($Header, "Number_1");
my $name = &parsingHeader($Header, "Name");
foreach(@Proto) {
my @fields = quotewords( ';', 0, $_);
$fields[$Nr] =~tr/0123456789/9876543210/ if defined $Nr;
$fields[$Nr_1] =~tr/0123456789/9876543210/ if defined $Nr_1;
$fields[$name] =~tr/A-Za-z/B-Wzb-w/ if defined $name;
push @Assembly, join(";",@fields);
}
unshift @Assembly, $Header;
print @Assembly;
}
sub parsingHeader {
my $x;
my $Row = shift @_;
my $Column = shift @_;
my @parts = split(/;/, $Row);
for(my $i = 0; $i <= @parts; $i++)
{
if($parts[$i] eq $Column) {
$x = $i;
last;
};
}
return $x;
}
__DATA__
Number;Name;Age;Gender;Number_1;Dummy
123;Andrew;54;m;123;AAA
234;John;43;m;234;JJJ
345;Helen;23;w;345;HHH
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.