perl help replacing commas and embedding values with ctrl characters -


i need tweaking perl script.

i've got input file comma separated values so:

to_em,from_em,flags,updated,marks xtr133823@xra.co.nz#hv,abc@def.com,16,2007-08-18 16:18:50,33 

the first row column names to_em from_em flags updated marks , following record values each column:

to_em = xtr133823@xra.co.nz#hv from_em = abc@def.com flags = 16 updated = 2007-08-18 16:18:50 marks = 33 

i creating unique value (md5), prefixed "__pkey__".

each column name starts ^e. each value starts ^a, including hex value. record end ^d.

i want final output file this:

__pkey__^ad41d8cd98f00b204e9800998ecf8427e^eto_em^axtr133823@xra.co.nz#hv^efrom_em^aabc@def.com^eflags^a16^eupdated^a2007-08-18 16:18:50^emarks^a33^e^d 

but, coming out this:

__pkey__^ad41d8cd98f00b204e9800998ecf8427e^e^ato_em^e^d__pkey__^a5c09354d0d3d34c96dbad8fa14ff175e^e^axtr133823@xra.co.nz#hv^e^d 

here's code:

use strict; use digest::md5 qw(md5_hex); $data = ''; while (<>) { $digest = md5_hex($data);    chomp;    ($val) = split /,/;    $data = $data. "__pkey__^a$digest^e^a$val^e^d"; } print $data; exit; 

this seems work:

use strict; use digest::md5 qw(md5_hex); $data = ''; $line1 = <>; chomp $line1; @heading = split /,/, $line1; #my ($sep1, $sep2, $eor) = (chr(1), chr(5), chr(4)); ($sep1, $sep2, $eor) = ( "^a", "^e", "^d"); while (<>) {     $digest = md5_hex($data);     chomp;     (@values) = split /,/;     $extra = "__pkey__$sep1$digest$sep2" ;     $extra .= "$heading[$_]$sep1$values[$_]$sep2" (0..$#values);     #$extra .= "$heading[$_]$sep1$values[$_]$sep2" (0..scalar(@values)-1);     #for $i (0..$#values)     #{     #   $extra .= "$heading[$i]$sep1$values[$i]$sep2";     #}     $data .= "$extra$eor"; } print $data; 

it reads first line, chomps it, , splits fields array @heading.

it reads each subsequent line, chomps it, splits fields, runs digest on it, , generates output line.

at end, prints accumulated data.

if want actual control characters instead of caret-letter, use line chr() instead of following one.

if don't all-on-one-line loop, use commented out one.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -