Solved

Easy Sorting Question

Posted on 2004-04-20
9
173 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 163
Perl 101 11 76
crawling gofundme 4 126
need a perl -nle command to split string with delimiter ( a delimiter can be another string) 2 86
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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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