?
Solved

Easy Sorting Question

Posted on 2004-04-20
9
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans
Suggested Courses

741 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