Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

wow.

abonghit1abonghit1 Posts: 71Member
Coming from a C programmer who just finished Larry Wall's book, there is but one thing to say about Perl:

Pure, Unadulterated, Blinding Brilliance.

My head is Spinning.

'unless' really threw me for a loop; I thought it was a typo!

Now for the question:

I'm looking to rewrite some of my C programs in order to practice Perl; however, most of the 'meaty' ones that I have access to at work involve sequential-file record parsing. I typically have a data structure that contains all the fields that I expect to see in the sequential file - then I read each record into a buffer, and memcpy() the buffer into the data structure (the structure mirrors the format of each record).
I have an array of these structures, with each item in the array containing the now-useful data from one record in the sequential file. Now I can use array[0].member to access some needed piece of data, or iterate through the whole shebang to dump the data to a database-friendly file.

Looking back on what I've learned from the Perl book, I would use an associative array to hold the data from each record. But this seems silly; I would have to
a) have an array of associative arrays (in C, an array of structs)
b) read each record into some variable
b) address each Key of each associative array [b]by name[/b] and copy the data from a certain chunk of the variable (using substr())into the Value piece of the associative array. And I don't even know how substr() would handle binary data (ascii 0 etc..).

Is C the proper language to do this efficiently? Or am I completely wrong about the proper way to do this using Perl? (I am hoping that the latter is true; I can't wait to get started)

Thanks!!!!
-Mike


Comments

  • JonathanJonathan Posts: 2,914Member
    : Coming from a C programmer who just finished Larry Wall's book, there
    : is but one thing to say about Perl:
    :
    : Pure, Unadulterated, Blinding Brilliance.
    :
    : My head is Spinning.
    :
    Ah, Larry Wall's book really helped me get better at Perl. I learnt C after Perl. Those two and JAVA are the languages I work with most of the time these days. And ML once in a while for fun... ;-)

    And if you think Perl 5 is cool, wait for Perl 6!

    : 'unless' really threw me for a loop; I thought it was a typo!
    :
    If saves an exclamation mark, which is easy to miss when reading code. unless is nice to have.

    : Now for the question:
    :
    : I'm looking to rewrite some of my C programs in order to practice
    : Perl; however, most of the 'meaty' ones that I have access to at work
    : involve sequential-file record parsing. I typically have a data
    : structure that contains all the fields that I expect to see in the
    : sequential file - then I read each record into a buffer, and memcpy()
    : the buffer into the data structure (the structure mirrors the format
    : of each record).
    Not what you're asking, but can't you just (char*) cast a pointer to the structure and read the data straight into it to save you a memcpy() (and a chunk of memory)? This is what I tend to do.

    : I have an array of these structures, with each item in the array
    : containing the now-useful data from one record in the sequential file.
    : Now I can use array[0].member to access some needed piece of data, or
    : iterate through the whole shebang to dump the data to a
    : database-friendly file.
    :
    : Looking back on what I've learned from the Perl book, I would use an
    : associative array to hold the data from each record. But this seems
    : silly; I would have to
    : a) have an array of associative arrays (in C, an array of structs)
    : b) read each record into some variable
    : b) address each Key of each associative array [b]by name[/b] and copy
    : the data from a certain chunk of the variable (using substr())into the
    : Value piece of the associative array. And I don't even know how
    : substr() would handle binary data (ascii 0 etc..).
    :
    And what about numbers? You'd have to use unpack to turn the packed binary data into a valid number. And then there's endianness...C structs are inherently platform dependent. And no, Perl strings are not null terminated, but if you have a chunk of data you can easily get rid of the null and the stuff that follows it:-
    $string =~ s/^(.+?)/$1/;
    Then substr will probably work as you hope.

    : Is C the proper language to do this efficiently? Or am I completely
    : wrong about the proper way to do this using Perl? (I am hoping that
    : the latter is true; I can't wait to get started)
    :
    Handling data like this is kinda annoying, and certainly more efficient in C. That said, it may be worth the work if writing the code to do the processing is greatly easier once you've got the data in.

    You'll probably want to look up the functions pack and unpack in the Perl documentation (unpack for reading in binary data, pack for writing it out). Alternatively, there is this module:-
    http://search.cpan.org/~amichauer/C-Include-1.40/Include.pm
    I've not used it, but it might make things much easier.

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.