felipon has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Reading different format records from an ASCII file
by TrekNoid (Pilgrim) on Jul 21, 2004 at 20:31 UTC
    Also, use the eq operator for character-based comparisons. == is a numeric comparison operator.

    Trek

      This observation, to use eq instead of the ==, fix the logic, THANKS!
        Any changes for using this script, trigged by an HTML form?
Re: Reading different format records from an ASCII file
by pbeckingham (Parson) on Jul 21, 2004 at 20:24 UTC

    If I space out the fields, I think the you'll see that there is no regularity in the number of fields:

    C~3453 | Kelly |Horton | Jockey | 27 | $110,250.30 N~3912 | Tom |Cat | $10,345.10
    Given the lack of regularity, you'd need to compensate for that in your split statement assignment, so that Tom's fourth field is equivalent to Kelly's sixth.

      The OP has this covered already. The difference in field layout is flagged by the initial character, and the OP's code already branches on that, doing a different split in each case.