Newbie with Sort and Merge Problem 
Author Message
 Newbie with Sort and Merge Problem

Hi!

    I'm very new to Perl (and, in fact, to newgroups), and I am having some
difficulties with a University programming assignment I've been given.  I
hope someone out there can help.  I have been given two separate files that
I need to sort and merge (not necessarily in that order).  The files contain
records that look like this:

00007650090000000000UOR001000000000900000000103/10/1999 0:00:00

The first 10 characters are a customer number, the second ten are a
connection time, and so on.  I need to be able to combine the records from
the two files and sort them.  The records will be sorted first by customer
#, then by connection time, and finally by a third field.

I am planning to read both files into a single array and use the sort
operator with a sort subroutine.  In the subroutine, I plan to break up the
records into fields using the substr() operator, and store each field in a
temporary string.  I will then evaluate the each field (if necessary) to
determine the overall order of each pair of records sent to the subroutine.

Will this work?  Is there a better way to do it?  Should I read both files
into a single array, or is there a better way to approach the merge?

Any comments or suggestions would be greatly appreciated.  Thanks for your
time!

Colin Popenia



Wed, 11 Aug 2004 23:28:29 GMT  
 Newbie with Sort and Merge Problem

Quote:

>     I'm very new to Perl (and, in fact, to newgroups), and I am having some
> difficulties with a University programming assignment I've been given.

When experiencing difficulties with school homework assignments,
you should consult with your peer mentor, your professor's teaching
assistant or your professor. This is both expected and appropriate
student conduct.

Asking outside of your classroom resources, is inappropriate.
None here can provide an answer within the context set by
your professor. You will learn less here. Asking here is to
do yourself a disservice; is to cheat yourself out of an
excellent learning opportunity, for which you or most likely,
your parents have paid hard earned egg money.

Godzilla!



Wed, 11 Aug 2004 23:46:10 GMT  
 Newbie with Sort and Merge Problem

Quote:

>     I'm very new to Perl (and, in fact, to newgroups), and I am having some
> difficulties with a University programming assignment I've been given.  I
> hope someone out there can help.  I have been given two separate files that
> I need to sort and merge (not necessarily in that order).  The files contain
> records that look like this:

> 00007650090000000000UOR001000000000900000000103/10/1999 0:00:00

> The first 10 characters are a customer number, the second ten are a
> connection time, and so on.  I need to be able to combine the records from
> the two files and sort them.  The records will be sorted first by customer
> #, then by connection time, and finally by a third field.

Not sure here. With first and second and finally you mean in case of
equality? Or do you mean that you actually create three different
records each sorted by another criterium?

Quote:
> I am planning to read both files into a single array and use the sort
> operator with a sort subroutine.  In the subroutine, I plan to break up the
> records into fields using the substr() operator, and store each field in a
> temporary string.  I will then evaluate the each field (if necessary) to
> determine the overall order of each pair of records sent to the subroutine.

Yes, this sounds sane.

Quote:
> Will this work?  Is there a better way to do it?  Should I read both files
> into a single array, or is there a better way to approach the merge?

Some notes:
You don't have to read the files into an array in beforehand, nor in two
arrays. The sort() routine (in its BLOCK LIST form) takes a list as
stuff to be sorted, hence it provides list-context to its operands.
Therefore a simple idiom would be:

    sort { sort-routine } <FILE1>, <FILE2>;

The sort-routine is probably the more interesting part. Keep the
following construct in mind. It will first do the comparison of the
first field and only if the comparison returns 0 (that is, equal) will
continue with comparing the second field and so on [pseudo-code]:

    sort {
            field1_a cmp field1_b
                     ||
            field2_a cmp field2_b
                     ||
                    ....        } LIST;

Depending on the nature of your date, you either need the cmp operator
for string-comparison or <=> (the spaceship-op) for numerical
comparison. If you swap the values referring to $a and $b you reverse
the order of your sorting.

As a matter of fact, all this is demonstrated quite excessively in
perlfaq4:

    Found in /usr/share/perl/5.6.1/pod/perlfaq4.pod
           How do I sort an array by (anything)?

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;



Thu, 12 Aug 2004 00:23:53 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. merging sorted lists...

2. inifinite recursion on my merge sort??

3. ascii file:extracting,sorting merging

4. request for a perl script for merge sort

5. Newbie: Need template script for merging text info from files

6. again a sort problem (help me please: newbie)

7. Newbie problem: Sorting ASCII flatfile databases

8. Sort of a sort problem.

9. re-sorting a sorted list (and sorting by date)

10. Newbie: Strange \n in sorted array...

11. newbie sort question

12. Newbie question on sort

 

 
Powered by phpBB® Forum Software