is it posible to append to txt file vertically?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    is it posible to append to txt file vertically?

    Is it posible to append to txt file vertically???
    Example original file... (constants: A, B , C, Car, Boat)
    Code:
    BLANK,	Car,	BLANK,	Boat,	BLANK
    BLANK,	Nov07,	Dec07,	Nov07,	Dec07
    A,	1,	2,	2,	7,
    B,	1,	2,	2,	7,
    C,	1,	2,	2,	7,
    when come to january, i wish to add the Jan 08 data (for noth car and boat respectively into the previous data), it should be adding to vertically right of the dec 07 data..

    Code:
    BLANK,	Car,	BLANK,	BLANK,	Boat,	BLANK,	BLANK
    BLANK,	Nov07,	Dec07,	Jan08,	Nov07,	Dec07,	Jan08
    A,	1,	2,	3,	2,	7,	4
    B,	1,	2,	4,	2,	7,	4
    C,	1,	2,	5,	2,	7,	5

    Thanks.
    Last edited by eWish; Mar 22 '08, 09:56 PM. Reason: Please use [code][/code] tags for data as well.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    I believe that there are some modules that will let you append the file in the middle. However, I think that you would have to read the entire line and add the data to it. Then append the file. I would search CPAN for the Tie::File module. Going off memory, so I could be wrong here.

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      It is possible but it is not a standard function or standard filehandle option. You would have to write code to append data to the ends of lines or ends of specific lines.

      Comment

      • MyMarlboro
        New Member
        • Mar 2008
        • 71

        #4
        Thanks for the inputs.
        Could anyone show me the code on how to add in the whole column to the file?

        ____Original text file___
        A,1,2
        B,1,2
        C,1,2
        D,1,2

        __column to be input__
        NEWA
        NEWB
        NEWC
        NEWD


        ___OutputFile__ _
        A,1,NEWA,2
        B,1,NEWB,2
        C,1,NEWC,2
        D,1,NEWD,2

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Originally posted by MyMarlboro
          Thanks for the inputs.
          Could anyone show me the code on how to add in the whole column to the file?

          ____Original text file___
          A,1,2
          B,1,2
          C,1,2
          D,1,2

          __column to be input__
          NEWA
          NEWB
          NEWC
          NEWD


          ___OutputFile__ _
          A,1,NEWA,2
          B,1,NEWB,2
          C,1,NEWC,2
          D,1,NEWD,2
          This can be done easily if you know the column number/field number where you want to insert data. Else, you have to calculate the field number by searching for a keyword(like 'Dec 07' in your initial example).
          For the above case, where data need to be inserted in 3rd column/field, the following code works:
          [CODE=perl]
          use strict;
          use warnings;

          open(DATA,"data .txt") or die "data open failed:$!";
          open(UPDATE,"up date.txt") or die "update open failed:$!";
          open(RES,">resu lt.txt") or die "create failed:$!";

          my @update;
          ##take data for updating into an array
          while(<UPDATE>) {chomp; push @update,$_ ;}
          while(<DATA>) {
          my @fields=split(/,/,$_); ##splitting on commas
          splice(@fields, 2,0,shift(@upda te)); ##inserting in position:3/index:2
          my $res=join(",",@ fields); ##joining elements with coma
          print RES $res;
          }
          close(DATA);clo se(UPDATE); close(RES);
          [/CODE]

          Comment

          Working...