sort a text file

G

Gabriel

Hello,

I need to DO a subrutine to sort a text file like this:
"ID";"name1";"name2";"surname";age;address;

I have done this one, and it is works ok, but just once. (?)
I think, it writes incorrectly into file, and then it simply can
understand it.


sub ordenar {
open(ORIGINAL_FILE, "<"."$_[0]");
open(SORT_FILE, ">"."$_[0]"."sort");
while ($linea_actual=<ORIGINAL_FILE>) {
@menor_linea = split /;/, $linea_actual;
while ($linea_actual=<ORIGINAL_FILE>) {
@linea_actual = split /;/, $linea_actual;
if ($menor_linea[$_[1]] gt $linea_actual[$_[1]]) {
@menor_linea = @linea_actual;
}
}
$linea =
"$menor_linea[0];$menor_linea[1];$menor_linea[2];$menor_linea[3];$menor_linea[4];$menor_linea[5]";
&eliminar_registro($_[0], $menor_linea[0]);
print SORT_FILE $linea;
close(SORT_FILE);
close(ORIGINAL_FILE);
open(SORT_FILE, ">>"."$_[0]"."sort");
open(ORIGINAL_FILE, "<"."$_[0]");
}
close(ORIGINAL_FILE);
close(SORT_FILE);
unlink($_[0]);
rename("$_[0]"."sort", $_[0]);
}
 
B

Brian McCauley

Hello,

I need to DO a subrutine to sort a text file like this:
"ID";"name1";"name2";"surname";age;address;

I have done this one, and it is works ok, but just once. (?)
I think, it writes incorrectly into file, and then it simply can
understand it.

sub ordenar {
open(ORIGINAL_FILE, "<"."$_[0]");
open(SORT_FILE, ">"."$_[0]"."sort");
while ($linea_actual=<ORIGINAL_FILE>) {
@menor_linea = split /;/, $linea_actual;
while ($linea_actual=<ORIGINAL_FILE>) {
@linea_actual = split /;/, $linea_actual;
if ($menor_linea[$_[1]] gt $linea_actual[$_[1]]) {
@menor_linea = @linea_actual;
}
}
$linea =
"$menor_linea[0];$menor_linea[1];$menor_linea[2];$menor_linea[3];$menor_linea[4];$menor_linea[5]";
&eliminar_registro($_[0], $menor_linea[0]);
print SORT_FILE $linea;
close(SORT_FILE);
close(ORIGINAL_FILE);
open(SORT_FILE, ">>"."$_[0]"."sort");
open(ORIGINAL_FILE, "<"."$_[0]");
}
close(ORIGINAL_FILE);
close(SORT_FILE);
unlink($_[0]);
rename("$_[0]"."sort", $_[0]);

}

That's very, very confused. You keep opening closing and re-reading
files.

If the file is big just use an external sort program. (Writing a sort
for big files is not something you should be bothered with). If the
file is not in a format that the external program will handle then use
Perl to tranform it into such a format and back.

If the file is small, slurp and use Perl's sort function.
 
G

Gabriel

Brian McCauley escribió:
Hello,

I need to DO a subrutine to sort a text file like this:
"ID";"name1";"name2";"surname";age;address;

I have done this one, and it is works ok, but just once. (?)
I think, it writes incorrectly into file, and then it simply can
understand it.

sub ordenar {
open(ORIGINAL_FILE, "<"."$_[0]");
open(SORT_FILE, ">"."$_[0]"."sort");
while ($linea_actual=<ORIGINAL_FILE>) {
@menor_linea = split /;/, $linea_actual;
while ($linea_actual=<ORIGINAL_FILE>) {
@linea_actual = split /;/, $linea_actual;
if ($menor_linea[$_[1]] gt $linea_actual[$_[1]]) {
@menor_linea = @linea_actual;
}
}
$linea =
"$menor_linea[0];$menor_linea[1];$menor_linea[2];$menor_linea[3];$menor_linea[4];$menor_linea[5]";
&eliminar_registro($_[0], $menor_linea[0]);
print SORT_FILE $linea;
close(SORT_FILE);
close(ORIGINAL_FILE);
open(SORT_FILE, ">>"."$_[0]"."sort");
open(ORIGINAL_FILE, "<"."$_[0]");
}
close(ORIGINAL_FILE);
close(SORT_FILE);
unlink($_[0]);
rename("$_[0]"."sort", $_[0]);

}

That's very, very confused. You keep opening closing and re-reading
files.

If the file is big just use an external sort program. (Writing a sort
for big files is not something you should be bothered with). If the
file is not in a format that the external program will handle then use
Perl to tranform it into such a format and back.

If the file is small, slurp and use Perl's sort function.


I would like! But My teacher is a little bit ridicuoulus.

How can I avoid to opening and closing files? seek?
 
G

Gabriel

Michele Dondi escribió:
seek() is one option. Slurping them in all at once is another one. We
recommend not to slurp files unless needed. This is one situation in
which it may be sensible. Back to your original question, you wrote:

: I need to DO a subrutine to sort a text file like this:
: "ID";"name1";"name2";"surname";age;address;

And you want to sort on what?

I give an argument to my sort subrutine which indicate the filed which I
want to sort on? (Sorry for the english)


Supposing you want to sort on each field
alphabetically on all of them but on ID and age, and with highest
priority to the left, a simple solution with a schwartzian transform
would be

sub ordenar {
my $file=shift;

What have $file inside when you have already done "$file=shift"?
 
T

Tad McClellan

while ($linea_actual=<ORIGINAL_FILE>) {


As a new programmer you should (quickly) adopt sensible style
guidelines to follow when you write your programs.

One such general guideline that is widely accepted is to use
whitespace to separate important program elements:

while ($linea_actual = <ORIGINAL_FILE>) {
or
while ( $linea_actual = <ORIGINAL_FILE> ) {

Because that makes in much easier for people to read and understand.

See:

perldoc perlstyle

for some more good ideas to adopt early on.

$linea =
"$menor_linea[0];$menor_linea[1];$menor_linea[2];$menor_linea[3];$menor_linea[4];$menor_linea[5]";


You can write that as:

$linea = join ';', @menor_linea[0..5];

Which is probably easier to read and understand.

Of course neither of them will work should your data end up having
five or seven fields, so it is probably better to write it to handle
any number of fields rather than only 6 fields:

$linea = join ';', @menor_linea;

&eliminar_registro($_[0], $menor_linea[0]);


You should not use the ampersand (&) character on most
subroutine calls.

perldoc -q "&"

What’s the difference between calling a function as &foo and foo()?

open(ORIGINAL_FILE, "<"."$_[0]");


You should always, yes *always*, check the return value from open().

See also:

perldoc -q vars

What’s wrong with always quoting "$vars"?

Then make that:

open( ORIGINAL_FILE, "<" . $_[0] ) or die "could not open '$_[0]' $!";
or
open( ORIGINAL_FILE, "<$_[0]" ) or die "could not open '$_[0]' $!";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top