Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inorder file name

Status
Not open for further replies.

newprogram

Programmer
Jul 27, 2006
8
0
0
US
Hi, All,

I have the dataset like this:
the question is: how to insert m2 between m1 and m10,
insert m3 between m2 and m10
insert m4 between m3 and m10....

orinigal dataset:
m1 m15 m20 m26 m31 m37
m10 m16 m21 m27 m32 m38
m11 m17 m22 m28 m33 m39
m12 m18 m23 m29 m34 m4
m13 m19 m24 m3 m35 m40
m14 m2 m25 m30 m36 m41....


the final dataset should be like that:

m1 m7 m13 m19 m25 m31 m37 m43
m2 m8 m14 m20 m26 m32 m38 m44
m3 m9 m15 m21 m27 m33 m39 m45
m4 m10 m16 m22 m28 m34 m40 m46
m5 m11 m17 m23 m29 m35 m41 m47
m6 m12 m18 m24 m30 m36 m42 m48...


I tried to use Sort function but doesn't work at all.

Thanks in advances,
JW
 
is that the lines of a file or something else? Let's see the code you have tried.

- Kevin, perl coder unexceptional!
 
The relevant documentation would be found here:


Code:
my @data = qw(
	m1   m15  m20  m26  m31  m37
	m10  m16  m21  m27  m32  m38
	m11  m17  m22  m28  m33  m39
	m12  m18  m23  m29  m34  m4
	m13  m19  m24  m3   m35  m40
	m14  m2   m25  m30  m36  m41
);

my @sorted = map { $_->[0] }
          sort { $a->[1] <=> $b->[1] }
          map { [$_, substr($_, 1)] }
          @data;
 
Hi,

Here are my codes.
Thanks again.



#!/usr/local/bin/perl -w

if ($#ARGV < 0)
{
print "$0 directory. $#ARGV\n";
exit;
}

my $dir = $ARGV[0];
print $dir, "\n";
unless (-e $ARGV[0]) {
print "$ARGV[0] does not exist\n";
exit;
}
my $file;
opendir (DIR , "$dir") or die "can not open dir";
@files = grep {/^i/ && -f "$dir/$_"} readdir(DIR);
my $cnt = 1;
my $tmp = "im"; # I can rename this part for new files
foreach $file (sort @files) {
print "$file\t";
rename ($file, "$tmp$cnt");
$cnt++;
}
exit (0);

 
It seems like you really don't care about the original file names.. as long as you end up with the files in order?

So you can take files m30,m45,m17 and rename them to m1,m2,m3? Or do I not understand what you are trying to do?


Travis
 
after this:

@files = grep {/^i/ && -f "$dir/$_"} readdir(DIR);

what is in @files?

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top