unpack query

J

Jack Penarth

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
 
T

Tad McClellan

Jack Penarth said:
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
 
A

Anno Siegel

Jack Penarth said:
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
 
J

Jack Penarth

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
 
J

Jack Penarth

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
 
A

Anno Siegel

Jack Penarth said:
(e-mail address removed) (Jack Penarth) wrote in message

news:<[email protected]>...

[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:
^^^^^^^^^^^^^^^
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
 
B

Ben Morrow

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
 
J

Jack Penarth

Ben Morrow said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top