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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm trying to get numbers. 1

Status
Not open for further replies.

Alfio75

Programmer
Oct 25, 2005
25
MX
Hello there guys.
I got a big problem here.
When I get output files these r called for example
1.dat
2.dat
3.dat
..

10.dat
...
600.dat

What i need is to get numbers like:
001.dat
002.dat
...
600.dat

this in the case i got 600 files, I think that when i"ll get
1000 files or more , this new case will need to get numbers:
0001.dat
0002.dat
...
1000.dat

Why all of this?
I need all those files inside another program that need that secuence to read files better.
Any advice?

That's just in the print statement.
How i need to modify next file?
*****************************************
open (INFILE, $infile);

my $number = 1;
open FH, ">$output_directory/$number.dat" or die "No puedo abrir [$number.dat]";


while(<INFILE>) {
chomp;
if(/\s*$/) {

close FH;
$number++;
open FH, ">$number.dat" or die "No se puede abrir [$number.dat]";
next;
}
print FH "sphere={<", join(",", split),"Re}",">\n";
}
close FH;

close FH;
close INFILE;
 
How about snippet...

Code:
#!/usr/bin/perl -w
use strict;
use warnings;

my $max = 5000;
my $file;

foreach $file (0..20) {
    $file = '0' x (length($max) - length($file)) . $file;
    print "$file.dat\n";
}
Add the same amount of zeros to the beginning of $file as the difference in length (think string) between $file and $max.



----

In need of programming book recommendations.
 
Code:
$length=8;
$file=67;
$fmtstr="%0".$length."d";
$file = sprintf($fmtstr, $file).".dat";
print $file;

HTH
--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
#!/usr/bin/perl -w
use strict;
# note that use warnings is block scoped while -w is global.

my $file = "0001";

foreach $file (0..20)
  {
  print "$file.dat\n";
  $file++;
  }

As long as you don't use $file in a numeric context, that will work. As soon as I did a while($file<20) it changed from string context to numeric and I got this:

0001.dat
2.dat
3.dat
etc
 
Thanks guys but what i'm needing is to rename 600 files whose names r 1.dat, 2.dat...10.dat, 11.dat...600.dat To

001.dat, 002.dat,003.dat...010.dat etc
that's because when i use another program i need to get named on that way.
So any advice
 
Hi Alfio.
Would I know about that other program by any chance?
Why don't you make that other program produce the right filenames in the first place?
All you need to do it use "sprintf" to zero pad the filename.


Trojan.
 
#!/usr/bin/perl -w
use strict;
my $number = 1;
open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
while(<DATA>) {
if(/^(?:OCHO)?\s*$/) {
close FH;
$number++;
open FH, ">$number.dat" or die "Failed to open file [$number.dat]";
next;
}
print FH "<", join(",", split),">,R$firstchar","\n";
}
close FH;
__DATA__
47 5.04786152982032E+00 4.23000449311482E+00 8.04732158465680E+00
62 8.06191440232750E+00 5.82244841863038E+00 4.95787603533274E+00
62 5.04638324236482E+00 8.33705252525774E+00 7.32433413587005E+00
62 2.04108475715077E+00 5.82001901588952E+00 5.05415800574768E+00
62 5.13925614436763E+00 8.40867652716315E+00 2.75714159286964E+00
62 2.61800142894251E+00 1.77738175002289E+00 5.02054486805876E+00
62 4.94928280233908E+00 4.07302530110812E+00 2.01973663276942E+00
62 7.22636291779700E+00 1.72292369993206E+00 5.01056990979596E+00
OCHO
47 5.04984575451512E+00 4.16899949437203E+00 8.38913015254607E+00
47 8.41311365271435E+00 5.90089795783486E+00 4.95471182062348E+00
47 5.03415065613991E+00 8.59685365002286E+00 7.55813423208373E+00
62 1.74698681832614E+00 5.90648520608937E+00 5.06338313646141E+00
62 5.18293202209829E+00 8.65797235431727E+00 2.55146839659064E+00
62 2.37498157493418E+00 1.52435602504604E+00 5.05472507160296E+00
62 4.94013649443927E+00 3.99148837619247E+00 1.70888099796613E+00
62 7.45281058614491E+00 1.44078631178572E+00 5.02800797156495E+00
*************\
I think trojan ur last code just need a light modification to make output files in the format i said before.

The program 'im telling u can make images from all this .dat files.
 
the above code will not work, $firstchar has never been declared (nor defined) with "my" anywhere in the script. Also, if you are going to 600, you could maybe start at 100 and go to 700 and avoid the zero padding issue all together.
 
That's a good point KevinADC.!
In fact is just important the secuence and not the start of count.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top