unpack query

Discussion in 'Perl Misc' started by Jack Penarth, Nov 24, 2003.

  1. Jack Penarth

    Jack Penarth Guest

    I use the following code snippet on a file extracted from a
    database(fixed length fields)before formatting the output for
    printing.

    Field3 is the prime record identifier and often contains duplicate
    entries. How can I modify my code so that only the first instance
    (record) is used and the remaining records ditched?

    Code snippet follows:

    while (<FILE>) {
    ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
    $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    $field14, $field15, $field16) = unpack($format1, $_)
    write(OUT_PUT);

    TIA

    John
     
    Jack Penarth, Nov 24, 2003
    #1
    1. Advertising

  2. Jack Penarth <> wrote:

    > Field3 is the prime record identifier and often contains duplicate
    > entries. How can I modify my code so that only the first instance
    > (record) is used and the remaining records ditched?



    The way the answer to your Frequently Asked Questions suggests.

    perldoc -q duplicate


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 24, 2003
    #2
    1. Advertising

  3. Jack Penarth

    Anno Siegel Guest

    Jack Penarth <> wrote in comp.lang.perl.misc:
    > I use the following code snippet on a file extracted from a
    > database(fixed length fields)before formatting the output for
    > printing.
    >
    > Field3 is the prime record identifier and often contains duplicate
    > entries. How can I modify my code so that only the first instance
    > (record) is used and the remaining records ditched?
    >
    > Code snippet follows:
    >
    > while (<FILE>) {
    > ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
    > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    > $field14, $field15, $field16) = unpack($format1, $_)
    > write(OUT_PUT);


    There should be a "my" in front of the parenthesis full of variables.
    You're not running under strict, are you? What about warnings?

    Instead of single scalars ($field1, ... $field16), it would
    be better to use an array "@field".

    Also, there is a semicolon missing after unpack(). Is this really your
    code? Don't re-type code, copy/paste it.

    To select unique keys, use a hash to keep track of which you have seen:

    my %seen;
    while ( <FILE> ) {
    my ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
    $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    $field14, $field15, $field16) = unpack($format1, $_);
    next if $seen{ $field3};
    write(OUT_PUT);
    $seen{ $field3} = 1;
    }

    Anno
     
    Anno Siegel, Nov 24, 2003
    #3
  4. Jack Penarth

    Jack Penarth Guest

    -berlin.de (Anno Siegel) wrote in message news:<bpt20n$rnv$-Berlin.DE>...
    > Jack Penarth <> wrote in comp.lang.perl.misc:
    > > I use the following code snippet on a file extracted from a
    > > database(fixed length fields)before formatting the output for
    > > printing.
    > >
    > > Field3 is the prime record identifier and often contains duplicate
    > > entries. How can I modify my code so that only the first instance
    > > (record) is used and the remaining records ditched?
    > >
    > > Code snippet follows:
    > >
    > > while (<FILE>) {
    > > ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
    > > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    > > $field14, $field15, $field16) = unpack($format1, $_)
    > > write(OUT_PUT);

    >
    > There should be a "my" in front of the parenthesis full of variables.
    > You're not running under strict, are you? What about warnings?
    >
    > Instead of single scalars ($field1, ... $field16), it would
    > be better to use an array "@field".
    >
    > Also, there is a semicolon missing after unpack(). Is this really your
    > code? Don't re-type code, copy/paste it.
    >
    > To select unique keys, use a hash to keep track of which you have seen:
    >
    > my %seen;
    > while ( <FILE> ) {
    > my ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
    > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    > $field14, $field15, $field16) = unpack($format1, $_);
    > next if $seen{ $field3};
    > write(OUT_PUT);
    > $seen{ $field3} = 1;
    > }
    >
    > Anno



    Thank you for your help. My own PC went up in a puff of smoke so I
    had to use a colleagues machine and the snippet was typed from memory
    as the query is quite urgent for me.

    I am very inexperienced with perl but I am learning more each day.

    Once again thanks.

    John
     
    Jack Penarth, Nov 24, 2003
    #4
  5. Jack Penarth

    Jack Penarth Guest

    (Jack Penarth) wrote in message news:<>...
    > -berlin.de (Anno Siegel) wrote in message news:<bpt20n$rnv$-Berlin.DE>...
    > > Jack Penarth <> wrote in comp.lang.perl.misc:
    > > > I use the following code snippet on a file extracted from a
    > > > database(fixed length fields)before formatting the output for
    > > > printing.
    > > >
    > > > Field3 is the prime record identifier and often contains duplicate
    > > > entries. How can I modify my code so that only the first instance
    > > > (record) is used and the remaining records ditched?
    > > >
    > > > Code snippet follows:
    > > >
    > > > while (<FILE>) {
    > > > ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
    > > > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    > > > $field14, $field15, $field16) = unpack($format1, $_)
    > > > write(OUT_PUT);

    > >
    > > There should be a "my" in front of the parenthesis full of variables.
    > > You're not running under strict, are you? What about warnings?
    > >
    > > Instead of single scalars ($field1, ... $field16), it would
    > > be better to use an array "@field".
    > >
    > > Also, there is a semicolon missing after unpack(). Is this really your
    > > code? Don't re-type code, copy/paste it.
    > >
    > > To select unique keys, use a hash to keep track of which you have seen:
    > >
    > > my %seen;
    > > while ( <FILE> ) {
    > > my ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
    > > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    > > $field14, $field15, $field16) = unpack($format1, $_);
    > > next if $seen{ $field3};
    > > write(OUT_PUT);
    > > $seen{ $field3} = 1;
    > > }
    > >
    > > Anno

    >
    >
    > Thank you for your help. My own PC went up in a puff of smoke so I
    > had to use a colleagues machine and the snippet was typed from memory
    > as the query is quite urgent for me.
    >
    > I am very inexperienced with perl but I am learning more each day.
    >
    > Once again thanks.
    >
    > John


    Just an update from me.

    As I said, I am inexperienced with perl and it would appear that I am
    not running under strict.

    However, with your help I was able to modify my code (I noticed the my
    %seen typo) and, because I realised that the identifier actually
    occupied two field, I was able to incorporate that as well as follows:

    > my $seen;
    > while ( <FILE> ) {
    > $field1, $field2, $field3, $field4, $field5, $field6, $field7,
    > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    > $field14, $field15, $field16) = unpack($format1, $_);
    > next if $seen{ $field3, $field4};
    > write(OUT_PUT);
    > $seen{ $field3, field4} = 1;
    > }


    It now works perfectly.

    Once again thanks for your help.

    John
     
    Jack Penarth, Nov 25, 2003
    #5
  6. Jack Penarth

    Anno Siegel Guest

    Jack Penarth <> wrote in comp.lang.perl.misc:
    > (Jack Penarth) wrote in message
    > news:<>...
    > > -berlin.de (Anno Siegel) wrote in message

    > news:<bpt20n$rnv$-Berlin.DE>...


    [big snip]

    > Just an update from me.
    >
    > As I said, I am inexperienced with perl and it would appear that I am
    > not running under strict.
    >
    > However, with your help I was able to modify my code (I noticed the my
    > %seen typo) and, because I realised that the identifier actually
    > occupied two field, I was able to incorporate that as well as follows:
    >
    > > my $seen;
    > > while ( <FILE> ) {
    > > $field1, $field2, $field3, $field4, $field5, $field6, $field7,
    > > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
    > > $field14, $field15, $field16) = unpack($format1, $_);
    > > next if $seen{ $field3, $field4};

    ^^^^^^^^^^^^^^^^
    > > write(OUT_PUT);
    > > $seen{ $field3, field4} = 1;

    ^^^^^^^^^^^^^^^
    There's a "$" missing before "field4". This is either a bug in your
    program, or you have been re-typing the code. Use copy/paste to
    transfer code to a news message.

    > > }

    >
    > It now works perfectly.


    Uh, oh. The highlighted syntax isn't standard Perl, it's a leftover from
    Perl 4 and shouldn't be used without understanding its limitations. I bet
    you just guessed the syntax might work, and it did. (Curses in the DWIMmer's
    general direction)

    What you are using is known as multidimensional hash emulation. It's
    described in _Programming Perl_ Ch. 2, Section _Hashes_. It's also
    in the online documentation, but offhand I don't know where.

    A better way to write it is

    $seen{ $field3}->{ $field4}

    which is a true (well, "truer", to quote the Camel) two-dimensional
    hash.

    Anno
     
    Anno Siegel, Nov 25, 2003
    #6
  7. Jack Penarth

    Ben Morrow Guest

    -berlin.de (Anno Siegel) wrote:
    > What you are using is known as multidimensional hash emulation. It's
    > described in _Programming Perl_ Ch. 2, Section _Hashes_. It's also
    > in the online documentation, but offhand I don't know where.


    perlvar, under $;

    Ben

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
     
    Ben Morrow, Nov 25, 2003
    #7
  8. Jack Penarth

    Jack Penarth Guest

    Ben Morrow <> wrote in message news:<bq0cqj$k37$>...
    > -berlin.de (Anno Siegel) wrote:
    > > What you are using is known as multidimensional hash emulation. It's
    > > described in _Programming Perl_ Ch. 2, Section _Hashes_. It's also
    > > in the online documentation, but offhand I don't know where.

    >
    > perlvar, under $;
    >
    > Ben


    Still haven't gotten my PC back so I am having to use vi (I like
    it!)under AIX on an IBM box to type the script and then re-type in the
    group, sorry about the typo's.

    You are right of course. Because of the urgency of the situation I
    needed to get the script working - it is called from a complicated
    legacy shell script that does lots of other things. With a bit of luck
    I will be able to find time to sit down with your comments and the
    documentation and understand it more fully.

    Thanks again.

    John
     
    Jack Penarth, Nov 26, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Lair
    Replies:
    0
    Views:
    410
    Christian Lair
    Feb 9, 2004
  2. Stacy Mader
    Replies:
    4
    Views:
    821
    Ekkehard Morgenstern
    Nov 22, 2003
  3. Matthew Barnes
    Replies:
    0
    Views:
    374
    Matthew Barnes
    Nov 19, 2003
  4. ahsan Imam
    Replies:
    4
    Views:
    5,729
    Josiah Carlson
    Feb 7, 2004
  5. Anton Hörnquist

    Array#pack and String#unpack query

    Anton Hörnquist, Sep 11, 2008, in forum: Ruby
    Replies:
    2
    Views:
    111
    Jan Dvorak
    Sep 11, 2008
Loading...

Share This Page