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

I need a Perl Code to transfer a pattern from file1 to file 2.I have two files one of them is a text file where I have pattern stored in in binary format, and another one is ASCII file where I have to transfer data.The pattern should be placed only after wor "PI" in ASCII file.

For eg :

File 1(.txt)-

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(ascii)-

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; pattern = 1; apply "grp1_load" 0 = chain "chain1" = "110"; end; force "PI" "110000101" 1; measure "PO" "11" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "100"; end; pattern = 2; apply "grp1_load" 0 = chain "chain1" = "001"; end; force "PI" "010000110" 1; measure "PO" "10" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "010"; end;

2018-01-26 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: Pattern transfer
by haukex (Archbishop) on Jan 24, 2018 at 13:46 UTC
Re: Pattern transfer
by tybalt89 (Monsignor) on Jan 26, 2018 at 13:39 UTC

    Grabbing a huge bucket-full of guesses, and using inline files for this test case.

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1207825 use strict; use warnings; open my $file1, '<', \<<END; # replace with your real open 10000000 10000000 10000000 10000000 10000000 00000011 00000011 00000011 00000011 00000011 10000001 10000001 10000001 10000001 10000001 11000000 11000000 11000000 11000000 11000000 01100000 01100000 01100000 01100000 01100000 END open my $file2, '<', \<<END; # replace with your real open 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; pattern = 1; apply "grp1_load" 0 = chain "chain1" = "110"; end; force "PI" "110000101" 1; measure "PO" "11" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "100"; end; pattern = 2; apply "grp1_load" 0 = chain "chain1" = "001"; end; force "PI" "010000110" 1; measure "PO" "10" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "010"; end; END my $contents2 = do { local $/; <$file2> }; while( <$file1> ) { my @replacements = split; print $contents2 =~ s/"PI" "\K\w+/ shift @replacements /ger; }

    Does the output match your desired output (which you didn't show us) ?

Re: Pattern transfer
by hippo (Archbishop) on Jan 24, 2018 at 13:46 UTC

    Hello jumdesumit. What did you try and how did it fail?