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

Dear gnarly people, I'm a new Perl Monk, having been programming Perl for several years. I've come across a situation: reading fixed-length data from one long input string per record, and getting fixed-length chunks out of it for mass-loading a MS-SQL database. This would be a perfect use for the FORTRAN EQUIVALENCE Statement, where for example, columns 1-20 of the input record would map to columns 1-20 of ONE, for example, where the memory used for columns 1-20 of the input record, would be also the SAME memory used for all 20 columns of ONE. Thanks!!

Replies are listed 'Best First'.
Re: EQUIVALENCE Stmt?
by Corion (Patriarch) on Apr 30, 2009 at 18:41 UTC

    I'm not sure what you actually want to do, but I do my fixed-length data munging using unpack and/or substr.

Re: EQUIVALENCE Stmt?
by pemungkah (Priest) on Apr 30, 2009 at 19:04 UTC
    HI from another former FORTRAN programmer.

    EQUIVALENCE was really great when you had a language that couldn't directly convert types worth anything, but you've got Perl, and it can manage this way better and simpler.

    You do indeed want to use unpack for this one. I'm guessing that your fixed-length data is in some binary format rather than in ASCII. unpack will let you take that in chunks and convert it - it's very like a FORMAT statement in that regard. However, it's a good bit more powerful, as it lets you have a lot of control as to how you want to manipulate the data.

    Take a look at unpack and pack for the lowdown on what you can do - it's quite a lot. I'm certain your need will be met.

Re: EQUIVALENCE Stmt?
by johngg (Canon) on Apr 30, 2009 at 21:38 UTC

    Yet another former FORTRAN programmer here!

    You might find some useful ideas in this node by BrowserUk.

    Cheers,

    JohnGG

Re: EQUIVALENCE Stmt?
by swampyankee (Parson) on Apr 30, 2009 at 22:47 UTC

    I've programmed Fortran for about thirty-five years (I started before the existence of Fortran-77, when FORTRAN didn't have a character data type). I've found the EQUIVALENCE statement to be a major source of buggery and non-portability. Indeed, it was banned in most of the shops in which I worked. I did use it a lot in PLM (product life-cycle management) programming using the Catia CDM/CDS API, and have used it for when I've needed to twiddle bits (yeah, I've done it in Fortran), but I've also had to maintain code where EQUIVALENCEd variables were used to read ASCII flat files, a practice which I believe is hideous, and completely unnecessary.

    Use pack and unpack. It's the closest you'll get to Fortran's FORMATted I/O


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

      Dear Corion, pemungkah, johngg, swampyankee, Thank you all so much for the replies.. I guess what I'm after is simply code simplification, the data is all character-based, no binary, i just wanted to do either EQUIVALENCE, or use the VMS Fortran's capability to do STRUCTUREs, so the the first 10 chars of INREC mapped directly to COLUMN1, the next 10 mapped directly to COLUMN2, and so forth. I've already done it with the following snippets.. $CDXCODE = NibbleItOff(8); where NibbleItOff is defined as:
      ###################################################################### +############### sub NibbleItOff { my $Field_Length = shift; my $Output_String; #print "NibbleItOff>>inrec->$inrec<\n"; print "NibbleItOff>>Field_Leng +th->$Field_Length<\n"; $Output_String = substr( $inrec, 0, $Field_Length ); $inrec = substr( $inrec, $Field_Length ); #print "NibbleItOff>>inrec->$inrec<\n"; print "NibbleItOff>>Output_Str +ing->$Output_String<\n"; return $Output_String; } ###################################################################### +###############
      using either EQUIVALENCE, or appropiate STRUCTUREs, that could be eliminated.. Just a passing thought... I'll gladly look up the UNPACK, but it seems to be for binary data, tho.. Thanks for all the hints!!! Jim
        Also Kudos to all the FORTRAN programmers (Or ex-FORTRAN)!!! It was and still is a wonderful language for math as well as whatever else it could do with extensions.. My flavor we used for the longest time was the DEC VAX line.. When we left the VAXen, after a long time of searching, I choose Perl for the next language, as it ran on VAX, PC, Unix, Linux, etc.. (no CP/M, tho! ;-) But that first computer is long gone...) Guess I just dated my self.. Now I've just written my first interface with MS-SQL Server..