mel00 has asked for the wisdom of the Perl Monks concerning the following question:

apl, Yes i realised that after i posted my reply...let me try to get what i had back up


File 1

------

trigger1|TEXT TO BE REPLACED

trigger2|TEXT TO BE REPLACED

trigger3|TEXT TO BE REPLACED

trigger4|TEXT TO BE REPLACED

trigger5|TEXT TO BE REPLACED

File 2

-------

combination1

combination2

combination3
Ouput.txt
---------

trigger1 combination1

trigger2 combination2

trigger3 combination3

trigger4 combination1

trigger5 combination2

My Code #!/usr/bin/perl my $infile1='test.txt'; my $infile2= ; my $outfile= 'output.txt'; open(INFO,"<$infile1") or die "could not open '$infile1; open(FILEA,"outputfromvariantparser1.txt") or die "could not open file +"; open(FILEB,"<<output.txt") or die "could not open file"; my @raw_data=<Filea>; while(my $line=<INFO>) { chomp($line); foreach $combination(@raw_dat) { $line=~s/TEXT TO BE REPLACED/$combination/s; } print Fileb $line; }

WADE - No this is not homework and i am new to perl and am just starting to learn it...and yes i know that my code is not doing what i want it to...it was only using the first combination from the second file and using that to substitute the text in the first file(tried to figure this out for a week before i got on here to ask for help). I was not sure if i was accessing the array elements correctly and thats why i was looking for help...thank you for your pointers i will keep that in mind.

jwkrahn: thank you so much for your help...can i ask how $raw_data[0] works?? i am not clear on how to access array elements and would appreciate it if you can explain that and lastly why is it that you are using 'push' and 'pop'.

Replies are listed 'Best First'.
Re: Copy data from one file and add it to another
by wade (Pilgrim) on Apr 25, 2008 at 22:11 UTC

    Hmmm. That looks like homework. You really should state that going in but you deserve kudos for giving it a shot before asking. Given that this is homework, though, I'm only going to give you hints.

    First, you should always:

    use strict; use warnings; use diagnostics;

    Next, you probably want to use the 3-argument version of open:

    open(INFO, "<", "$infile") or die ("could not open file to append".$!) +; open(FILEA, "<", "outputfromvariantparser1.txt") or die ("could not op +en file to write output"); open(FILEB,">>", output.txt") or die ("could not open file to write ou +tput");

    You should probably lookup 'open' -- I see a problem with more than one of your files. (hint: one of the translations I did, above, does what your original code did but clearly doesn't do what you intend).

    I don't think you want to nest your loops.

    You'll need to figure out how to cycle through 'raw_data'

    Edit: I made one hint a little clearer.

    --
    Wade
Re: Copy data from one file and add it to another
by jwkrahn (Abbot) on Apr 26, 2008 at 02:50 UTC
    #!/usr/bin/perl use warnings; use strict; my $infile = 'test.txt'; open INFO, '<', $infile or die "could not open '$infile' $!"; open FILEA, '<', 'outputfromvariantparser1.txt' or die "could not ope +n 'outputfromvariantparser1.txt' $!"; open FILEB, '>>', 'output.txt' or die "could not open 'output.txt' $!" +; my @raw_data = <FILEA>; while ( my $line = <INFO> ) { $line =~ s/TEXT TO BE REPLACED.*/$raw_data[0]/s; print FILEB $line; push @raw_data, shift @raw_data; }

      Nice solution. Liked particularly the way you make the array go on circles. There is one thing though. Given that input data the regexp is not considering the |. Maybe $line =~ s/\| TEXT TO BE REPLACED.*/ $raw_data[0]/s;

      I have two files one is a Text file where I have some different Patterns and another one is ASCII file where I have code to generate a test pattern. I need Perl Script where I can transfer data pattern to that ASCII file, after particular word "PI".

      File 1-

      Result is : 10000000 10000000 10000000 10000000 10000 +000 00000011 00000011 00000011 00000011 00000011 10000001 10000001 10000001 10000001 10000001 11000000 11000000 11000000 11000000 11000000 01100000 01100000 01100000 01100000 01100000

      File 2-

      CHAIN_TEST = pattern = 0; apply "grp1_load" 0 = chain "chain1" = "010"; end; force "PI" "XX0XXXXXX" 1; measure "PO" "XX" 2; apply "grp1_unload" 3 = chain "chain1" = "010"; end; pattern = 1; apply "grp1_load" 0 = chain "chain1" = "101"; end; force "PI" "XX0XXXXXX" 1; measure "PO" "XX" 2; apply "grp1_unload" 3 = chain "chain1" = "101"; end; end; SCAN_TEST = pattern = 0; apply "grp1_load" 0 = chain "chain1" = "010"; end; force "PI" "100000000" 1; measure "PO" "10" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "010"; end;

      2018-01-25 Athanasius added code and paragraph tags

        Put a unique marker in the file eg <PATTERN> where you want the substitution.

        #!/usr/bin/perl use strict; use warnings; my $infile1 = 'file1.txt'; my $infile2 = 'file2.txt'; my $outfile = 'result.txt'; #open IN,'<',$infile1 or die "$infile1 : $!"; #my @file1 = <IN>; #chomp(@file1); #close IN; my @file1 = qw(10000000 00000011 10000001 11000000 01100000 ); #open IN,'<',$infile2 or die "$infile2 : $!"; #my @file2 = <IN>; #close IN; my @file2 = <DATA>; open OUT,'>',$outfile or die "$outfile : $!"; for my $pattern (@file1){ print "Test pattern is $pattern\n"; for (@file2){ my $line = $_; # copy to preserve @file2 $line =~ s/<PATTERN>/$pattern/; print OUT $line; } } close OUT; __DATA__ SCAN_TEST = pattern = 0; apply "grp1_load" 0 = chain "chain1" = "010"; end; force "PI" "<PATTERN>" 1; measure "PO" "10" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "010"; end;
        poj
Re: Copy data from one file and add it to another
by apl (Monsignor) on Apr 26, 2008 at 14:48 UTC
    Slightly off-topic -- you shouldn't remove your original question and replace it with comments to the people who have responded. It is confusing to those of us who are just now approaching the node and see no question.

    Better form is to add at the end of your original question Revised: new comments.