Loading ...
Sorry, an error occurred while loading the content.
 

creating a 2 files from data in another file

Expand Messages
  • Lisa
    Hi Group, I am new to Perl. I am trying to write a program that reads in a file and then checks the line length. based on the lenght, data is written to 1 of
    Message 1 of 2 , Feb 4, 2004
      Hi Group,

      I am new to Perl. I am trying to write a program that reads in a file
      and then checks the line length. based on the lenght, data is written
      to 1 of two files. The problem is that although I've entered say the
      number 2 the file that should contain data for a file length less
      than 2 has data greater than two and nothing is done with the first
      line of the input file. what i would really like to do is to read in
      the file, check to see if there less than 2 tabs before the new line
      and if there are write that line to a file and if there are more than
      2, write that line to a separate file. Is there anyway to to this in
      Perl? Any help you can give is greatly appreciated:

      this is the code:

      #!c:\Perl\pearl -w

      # \\ is used to escape the \ special charater

      chdir("C:\\shortTermProjects\\") || die "can't change directory";


      #this will open the file if it is in the current directory

      open(INFILE,"<Trade.txt") || die "file can't be opened $!";
      do {

      $inputfile = <INFILE>;

      $length = length($inputfile);

      if ($length >6)
      {
      open (OUTFILE, ">>writetest.txt")|| die "can't write to file
      or file not found $!"; #creates a new file
      print OUTFILE;

      }
      }

      while (<INFILE>);

      close (INFILE) || die "can't close the INFILE files $!";
      open(INFILE,"<Trade.txt") || die "file can't be opened $!";
      do
      {
      $inputfile2 = <INFILE>;

      $length2 = length($inputfile2);
      if ($length2 < 6)
      {
      open (OUTFILE2, ">>dumptest.txt") || die "cant write to the
      dumptestfile or the file wasn't found $!";
      print OUTFILE2;

      }

      }

      while (<INFILE>);


      close (INFILE) || die "can't close the INFILE files $!";
      # close (OUTFILE)|| die "can't close the OUTFILE files $!";
      # close (OUTFILE2)|| die "can't close the OUTFILE2 files $!";
      print "\nfile closed";
    • Lisa
      Please ignore, I ve figured out the problem. ... file ... written ... the ... in ... line ... than ... in
      Message 2 of 2 , Feb 5, 2004
        Please ignore, I've figured out the problem.

        --- In perlforbeginners@yahoogroups.com, "Lisa" <ms_lisac@y...> wrote:
        > Hi Group,
        >
        > I am new to Perl. I am trying to write a program that reads in a
        file
        > and then checks the line length. based on the lenght, data is
        written
        > to 1 of two files. The problem is that although I've entered say
        the
        > number 2 the file that should contain data for a file length less
        > than 2 has data greater than two and nothing is done with the first
        > line of the input file. what i would really like to do is to read
        in
        > the file, check to see if there less than 2 tabs before the new
        line
        > and if there are write that line to a file and if there are more
        than
        > 2, write that line to a separate file. Is there anyway to to this
        in
        > Perl? Any help you can give is greatly appreciated:
        >
        > this is the code:
        >
        > #!c:\Perl\pearl -w
        >
        > # \\ is used to escape the \ special charater
        >
        > chdir("C:\\shortTermProjects\\") || die "can't change directory";
        >
        >
        > #this will open the file if it is in the current directory
        >
        > open(INFILE,"<Trade.txt") || die "file can't be opened $!";
        > do {
        >
        > $inputfile = <INFILE>;
        >
        > $length = length($inputfile);
        >
        > if ($length >6)
        > {
        > open (OUTFILE, ">>writetest.txt")|| die "can't write to file
        > or file not found $!"; #creates a new file
        > print OUTFILE;
        >
        > }
        > }
        >
        > while (<INFILE>);
        >
        > close (INFILE) || die "can't close the INFILE files $!";
        > open(INFILE,"<Trade.txt") || die "file can't be opened $!";
        > do
        > {
        > $inputfile2 = <INFILE>;
        >
        > $length2 = length($inputfile2);
        > if ($length2 < 6)
        > {
        > open (OUTFILE2, ">>dumptest.txt") || die "cant write to the
        > dumptestfile or the file wasn't found $!";
        > print OUTFILE2;
        >
        > }
        >
        > }
        >
        > while (<INFILE>);
        >
        >
        > close (INFILE) || die "can't close the INFILE files $!";
        > # close (OUTFILE)|| die "can't close the OUTFILE files $!";
        > # close (OUTFILE2)|| die "can't close the OUTFILE2 files $!";
        > print "\nfile closed";
      Your message has been successfully submitted and would be delivered to recipients shortly.