473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Sorting filenames

11
Hi,
I started using Perl a few days ago on Linux and have a bit of a problem. I have created a code which reads the content of every file in a directory, which works, but reads them in the wrong order. I have been searching for a function which reads the files in numerical order according to the filename (the letters in each filename are the same but the numbers increase by one each time). This is the code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. opendir (DH, "directory");
  3. while ($file = readdir(DH))
  4. {
  5.     open (FH, $file);
  6.     while(<FH>)
  7.     {
  8.         chomp;
  9.         print "$_\n";
  10.     }
  11. }
  12. close (FH);
  13.  
Any help would be appreciated, thanks in advance.
Oct 3 '07 #1
3 3224
numberwhun
3,509 Expert Mod 2GB
Hi,
I started using Perl a few days ago on Linux and have a bit of a problem. I have created a code which reads the content of every file in a directory, which works, but reads them in the wrong order. I have been searching for a function which reads the files in numerical order according to the filename (the letters in each filename are the same but the numbers increase by one each time). This is the code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. opendir (DH, "directory");
  3. while ($file = readdir(DH))
  4. {
  5.     open (FH, $file);
  6.     while(<FH>)
  7.     {
  8.         chomp;
  9.         print "$_\n";
  10.     }
  11. }
  12. close (FH);
  13.  
Any help would be appreciated, thanks in advance.
Well, one thing you could do, per the readdir page, is to read the directory listing with readdir into a list (array). That way it puts the entire listing into an array. Once it is in the array you can use the sort function to sort it, then read it one element at a time.

Also, when using readdir, it is customary and proper to use closedir() when you are done. It isn't required, but it is good coding practice.

Regards,

Jeff
Oct 3 '07 #2
KevinADC
4,059 Expert 2GB
but reads them in the wrong order
It is reading them in the order the system has them stored in. That is rarely any type of logically sorted order to humans. If you use an application to see the files on your computer ( a file manager application ) that application generally defaults to sorting files by name which is easiest for humans to find things.

When you use perl to read the files in a directory you have to write your own sorting function.
Oct 3 '07 #3
dh87lfc
11
Hi,
thanks for the help. I put the filenames into an array and used the sort function and it worked.
Oct 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: C. Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code:
1
by: Connelly Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code: ...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
1
by: xrow | last post by:
Hello I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control I have problem when sorting data in datagrid I have created...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
17
by: Cameron Walsh | last post by:
Hi, I have two lists, A and B, such that B is a subset of A. I wish to sort A such that the elements in B are at the beginning of A, and keep the existing order otherwise, i.e. stable sort. ...
9
by: Wolfgang Draxinger | last post by:
H folks, I got, hmm not really a problem, more a question of elegance: In a current project I have to read in some files in a given directory in chronological order, so that I can concatenate...
6
by: Soren | last post by:
Hi, I'd like to read the filenames in a directory, but not the subdirectories, os.listdir() gives me everything... how do I separate the directory names from the filenames? Is there another way...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.