Solved

Easy Sorting Question

Posted on 2004-04-20
9
154 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hard perl script 16 146
pattern matching in perl 2 100
iSeries PERL Scripts 7 132
Regular Expression question to filter with negation. 6 83
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 …
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

20 Experts available now in Live!

Get 1:1 Help Now