Solved

Easy Sorting Question

Posted on 2004-04-20
9
165 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now