Solved

Easy Sorting Question

Posted on 2004-04-20
9
180 Views
Last Modified: 2010-03-04
How can I change this code so that the COUNTRYOUTPUT file is sorted by $COUNTRY_NAME, the second item?

Thanks for your help!
Alex



sub saveCountry {

      ### Get number from and update $CountryNumber file

      open (FILE, "$CountryNumber"); @temp=<FILE>; close FILE;
 
      $COUNTRY_NUMBER = $temp[0]+1;

      open (FILE, ">$CountryNumber"); flock(FILE,2); print FILE "$COUNTRY_NUMBER"; flock(FILE,8); close FILE;

      ### Add the new profile to $CountryData

      $COUNTRY_DATA_LINE = "$COUNTRY_NUMBER|$COUNTRY_NAME|||||||||";

      &getCountryDataArray;


      open(COUNTRYOUTPUT, ">$CountryData");

      flock(COUNTRYOUTPUT,2);

      print COUNTRYOUTPUT "$COUNTRY_DATA_LINE\n";        # print the new country at the top of the country list
      print COUNTRYOUTPUT @countries;             # print all of the old country data



      flock(COUNTRYOUTPUT,8);

      close (COUNTRYOUTPUT);
}
0
Comment
Question by:alexswebsite
  • 4
  • 3
  • 2
9 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 10866774
print COUNTRYOUTPUT map{/[^|]*\|(.*)/s}
                                    sort
                                    map{join'|',(split/\|/,$_)[1],$_}
                                    @countries;
0
 
LVL 84

Expert Comment

by:ozo
ID: 10866931
(You shouldn't un-flock before you close.  If the last  buffer hasn't been flushed yet, it may allow another process to get the file before the close flushes the buffer)
0
 
LVL 84

Expert Comment

by:ozo
ID: 10866969
Also, the
    open(COUNTRYOUTPUT, ">$CountryData");
will truncate the file before you get a chance to flock it.
You should probably do
    open(COUNTRYOUTPUT, "+<$CountryData");
     flock(COUNTRYOUTPUT,2);
     truncate COUNTRYOUTPUT,0;
instead
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:alexswebsite
ID: 10867026
This code works to sort the array and store it in the file $CountryData, but how can I add $NEW_COUNTRY_ROW to the @countries array before doing the sorting and saving?  Thanks for your help ozo!

$NEW_COUNTRY_ROW = "$COUNTRY_NUMBER|$COUNTRY_NAME|||||||||\n";

&getCountryDataArray;  #gets data from $CountryData file
                                   #and stores it in @countries array

open(COUNTRYOUTPUT, ">$CountryData");
flock(COUNTRYOUTPUT,2);

print COUNTRYOUTPUT map{/[^|]*\|(.*)/s}
                                    sort
                                    map{join'|',(split/\|/,$_)[1],$_}
                                    @countries;

flock(COUNTRYOUTPUT,8);
close (COUNTRYOUTPUT);
0
 
LVL 8

Expert Comment

by:davorg
ID: 10868890
$NEW_COUNTRY_ROW = "$COUNTRY_NUMBER|$COUNTRY_NAME|||||||||\n";

&getCountryDataArray;  #gets data from $CountryData file
                                   #and stores it in @countries array

open(COUNTRYOUTPUT, "+<$CountryData");
flock(COUNTRYOUTPUT,2);
truncate COUNTRYOUTPUT,0;

print COUNTRYOUTPUT map{/[^|]*\|(.*)/s}
                                    sort
                                    map{join'|',(split/\|/,$_)[1],$_}
                                    @countries, $NEW_COUNTRY_ROW;

close (COUNTRYOUTPUT);
0
 

Author Comment

by:alexswebsite
ID: 10897223
The sorting is working fine for me, but adding the row with the new country isn't.  Here's the (simplified) code I'm trying to use to add the country data to the @countries list before I sort it using the code from ozo.

                $COUNTRY_NAME = $form{'country_name'};
      $NEW_COUNTRTY = "14|$COUNTRY_NAME|||||||||";
      
      @new_country = ($NEW_COUNTRY);

                open (DATA,"$CountryData");
      @countries = <DATA>;
      close (DATA);

      push(@countries, @new_country);


sample of $CountryData file:

2|Mexico|||||||||
3|Colombia|||||||||
4|Czech Republic|||||||||
5|Canada|||||||||
6|United Kingdom|||||||||
7|Chile|||||||||
8|Israel|||||||||
9|Italy|||||||||

When I was playing with this yesterday, I got it to add a new row onto the bottom of the file, but it did not fill in the variable from the form.  Now it won't even add the new row to the bottom.  Sorry if my code or question is confusing...

Thanks a lot of all the help!
Alex
0
 
LVL 8

Accepted Solution

by:
davorg earned 250 total points
ID: 10898312
You're doing some slightly strange and unnecessary things there, but I can't see anything that will stop it working.

Here's how I'd write it:

use Fcntl ':flock';

$NEW_COUNTRY = "14|$form{country_name}|||||||||";

open (CNTY, "+<$CountryData") or die $!;
flock CNTY, LOCK_EX or die $!;

@countries = <CNTY>, $NEW_COUNTRY;

truncate CNTY, 0;

print CNTY map{/[^|]*\|(.*)/s}
                sort
                map{join'|',(split/\|/,$_)[1],$_} @countries;

close CNTY;

I've renamed your filehandle to CNTY as DATA is a special filehandle in Perl.

I'd also recommend turning on "use strict" and "use warnings" to catch basic errors like mistyping variable names.

Dave...
0
 

Author Comment

by:alexswebsite
ID: 10935688
Thanks for all of your advice.  I've definately got a lot to learn...

I'm not sure why I'm having so much trouble with this.  I tried your code davorg, and it's just adding a blank line to the top of my file.

Any other advice?

Thank you,
Alex
0
 

Author Comment

by:alexswebsite
ID: 10935708
I've realized that my initial question was "how do I sort this?" but it's evolved quite a bit because once I got things sorted, I couldn't add the data I needed to my file.  Weird....

Anyhow, since my original question was answered by ozo, I'm awarding him the original 250 points.  

Since davorg has done a lot to help me since with the evolution of my problem, I'm also awarding him 250 points.

Thanks guys.  If you still have advice about my previous post, please let me know.

Alex
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question