Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
(Edit: See Sorting with perl for a reformatted version of the question, and other answers).

I'm going to try to answer your question, but it would have been easier if you'd used the <code> and a few <p>aragraph tags to make it more readable. Please check out Writeup Formatting Tips.

1st file has words that are in specific order like this: (not alphabetic) GAVSTE GAVARC GAVADA GAVIMM GAVSP

So you open up the first file, and read it into a hash that defines the sort order: (I'm assuming there's newlines between the words in the first file)

#/usr/bin/perl -w use strict; my %sortOrder; # replace "file1" with the filename of the first file my $currentOrder=0; open(FILE1,"<file1") or die "Can't open 'file1', error $!"; while(<FILE1>) { chomp; # associate the current word with its place in the sort order $sortOrder{$_}=$currentOrder++; } close(FILE1);
It is "order quide" for sorting 2nd file in flatfile (delimiter is "|"): GAVARC 10.3. Tahkoluoto 1m SOMMOL 10.3. Tahkoluoto 7m GAVSTE 7.4. Preiviiki 1p GAVARC 7.4. Preiviiki 2p SOMMOL 16.3. Kallo 6m

I have to guess at where the newlines are, so I'm going to assume the 2nd file looks like:

GAVARC|10.3.|Tahkoluoto|1m SOMMOL|10.3.|Tahkoluoto|7m GAVSTE|7.4.|Preiviiki|1p GAVARC|7.4.|Preiviiki|2p SOMMOL|16.3.|Kallo|6m
(continued from previous code:)

my @data; open(FILE2,"<file2") or die "Can't open 'file2', error $!"; while(<FILE2>) { chomp; my ($keyField)=split /\|/,$_; die "Don't have a sort key for $keyField" unless exists $sortOrder +{$keyField}; # push in a reference to an array where the first # element is the sort order. This is the Scwartzian # transform sorting method push @data,[$sortOrder{$keyField},$_]; } close(FILE2); my @sorted=map {pop @$_} sort {$a->[0] <=> $b->[0]} @data; # I'm assuming you want the | delimiters in the output? print join "\n", @sorted;
For more details see Schwartzian Transform. A higher performance approach is the Guttman Rosler Transform but it's a bit more complicated to follow.
--
Mike

In reply to Re: Sorting with perl by RMGir
in thread Sorting with perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-16 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found