I using ActiveState Perl version 5.8.4.810 and got an ASCII file with different length and format records, some of these fields with other languages edit chars, also widly used characters in PERL, (ie $).

How can I do this, also if using unpack, as well as, implications by using this script via CGI?

The test data file:

C~1212|Jan|Jaspree|Painter|38|$100,500.50
C~3453|Kelly|Horton|Jockey|27|$110,250.30
N~4312|Mario|Galvan|$100,100.40
N~3912|Tom|Cat|$10,345.10
N~1010|Micky|Mouse|$12.50

This following script set the wrong values for the scalar variables.

use Sed; open(FILE, "<fields01.dat"); while(<FILE>) { $_ = sed { s/\$//g } $_; $_ = sed { s/,//g } $_; ($rectype, $_) = split(/\~/, $_); printf("%s : %s\n", $rectype, $_); if ($rectype == "C") { ($empno, $fName, $lName, $job, $age, $salary) = split(/\|/, $_ +,6); printf("Employee NO:%s Name:%s FamilyN:%s Job:%s Age:%d Salary +:%10.2f\n", $empno, $fName, $lName, $job, $age, $salary); } else { ($empno, $fName, $lName, $salary) = split(/\|/, $_,4); printf("Employee NO:%s Name:%s FamilyN:%s Salary:%10.2f\n", $empno, $fName, $lName, $salary); } } close(FILE);
I appreciate your support!

In reply to Reading different format records from an ASCII file by felipon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.