Hey Vitali,
I am also a beginner, so I don't know if I am doing everything most efficiently...
but, here is what I wrote to do this. I wouldn't normally just write a program for
someone, but the problem intrigued me. Take a look through the code and it will
hopefully make sense. I hope it will at least get you going on the right road!
Jared
<begin code>
$infile = "input_file.txt";
if (! -e $infile) # if the input file doesn't exist, die
{
die "File $infile not found!";
}
open(JARREAD,$infile); # open the file with a filehandle I made up
while ($line=<JARREAD>) # reads each line of the file until done. notice "="
{
chop $line;
if (substr($line,0,2) eq "10")
{
$outfile=substr($line,3,4).".out"; # grab the file name and append a .out
if ($prefix > 0) # if there is no prefix, this is the first time through.
{
close(JARWRITE);
}
$prefix=substr($line,0,2); # prefix is what we add to the beggining of each line.
open(JARWRITE, ">>", $outfile);
print {JARWRITE} "$line\n"; # print the first line with no prefix
}
else
{
if (substr($line,0,2) > 10) # this says it found the next section ie 20
{
$prefix=substr($line,0,1); # make the prefix 1 character
# for each section label 10,20,etc, do not do a prefix
print {JARWRITE} "$line\n";
}
elsif (substr($line,0,2) eq "AB" or substr($line,0,2) eq "EP" or substr($line,0,2)
eq "A0")
{
# if we found one of the above sections titles, just spit out the line
print {JARWRITE} "$line\n";
}
else
{
# this substr is because the first char is a space for all other lines
$line=substr($line,1);
print {JARWRITE} "$prefix"."$line\n";
}
}
}
close(JARREAD); #close the files
close(JARWRITE);
<end code>
-----Original Message-----
From: Vitali Pokrovski [mailto:[EMAIL PROTECTED]]
Sent: Thursday, November 21, 2002 2:39 AM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Working with text files.Please help!
Dear friends,
could you please show me the way,how to write the code for converting data from
input_file to output files (1001.out,1002.out,1003.out) format.(please see an example
attached)
I have already doing with this code writing about four week,but
unfortunatlly..,becouse i'm just beginner:-(
I have a text file (input_file.txt). And output i need are files
1001.out;1002.out;1003.out.
Here are steps what programm must do:
1.Open input file
2.reads lines from canal "10"- to canal "A0" (info from line "10" to line "A0" must
be one file)
3.convert lines(canal numbers "10","20","30" ... on two first position) as is show in
output files. For example:
10
20
20
20
20
30
30
30
4.and save this data to new file(1001.out). P.S. File names coming from lines "10
1001" ,acctually file names is from 3 position for characters,in this case is "1001"
or "1002" or "1003"..
I'm not sure what I'm trying to do here..
#!perl -w
$count = 0;
while (<STDIN>) {
if (substr($_,0,2) eq "10") { #If left 2
++$count;
$lines = 0;
open OUT, ">$count.out";
print OUT $_; };
}
__EOF__
Any help welcome!
Regards,
VItali
Estonia,Tallinn
____________________________________________________
IncrediMail - Email has finally evolved - Click Here
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]