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

I have two files

File A

test "r5"

test "r1"

test "r1" version "10"

test "r1" version "11"

test "r1" version "12"

skip "r1" version "15"

test "r4"

test "r4" version "10"

skip "r4" version "11"

test "r3"

skip "r3" version "11"

test "r10"

skip "r10" version "11"

File B

r1

r10

r5

I would like to insert new version "13" into file A

The expected output:

test "r5"

test "r5" version "13"

test "r1"

test "r1" version "10"

test "r1" version "11"

test "r1" version "12"

test "r1" version "13"

skip "r1" version "15"

test "r4"

test "r4" version "10"

skip "r4" version "11"

skip "r4" version "13"

test "r3"

skip "r3" version "11"

skip "r3" version "13"

test "r10"

skip "r10" version "11"

test "r10" version "13"

File A,B don't have blank lines

Thanks in advance

10

Replies are listed 'Best First'.
Re: Insert lines in a file
by davido (Cardinal) on Mar 16, 2014 at 16:11 UTC

    You're going to have to explain how File B relates to File A. I don't want to guess (possibly incorrectly) how adding:

    r1 r10 r5

    To...

    test "r5" test "r1" test "r1" version "10" test "r1" version "11" test "r1" version "12" skip "r1" version "15" test "r4" test "r4" version "10" skip "r4" version "11" test "r3" skip "r3" version "11" test "r10" skip "r10" version "11"

    ...could result in the addition of these three lines:

    test "r5" version "13" ... skip "r3" version "13" ... test "r10" version "13"

    Is it a matter of finding the lowest unused "version" number greater than 10? Forget the file handling part... first we need to understand the recipe, or the rules for the change. The insertion issue can't even be addressed until we know how to derive what needs to be inserted, and where.

    If this data format has a name, or if you can name its source, that might also help, because things with names or with known sources often have predictable rules.

    We might also feel more motivated to help with a question that describes the need more thoroughly, and that shows (with real code) what you've already tried.

    Also, if you're wondering why my formatting of your file data looks better than yours, it's because I wrapped it in <code>...</code> tags, per Writeup Formatting Tips.


    Dave

      If exist in file B add "test" else add "skip"

Re: Insert lines in a file
by Lennotoecom (Pilgrim) on Mar 16, 2014 at 19:14 UTC
    That's how I understood your task.
    Though you'd better follow davido's advice.
    use Inline::Files; $v = 'r13'; %b = map {chomp; $_ => undef} <FILEB>; while (<FILEA>){ @t = split /[\s|\"]/; push @a, $t[2] if ! exists $h{$t[2]}; $h{$t[2]}{$t[6]} = $t[0]; } foreach (@a){ exists $b{$v} ? ($h{$_}{$v} = "test") : ($h{$_}{$v} = "skip"); foreach $key (sort keys $h{$_}){ $key ne '' ? print "$h{$_}{$key} $_ version $key\n" : print "$h{$_}{$key} $_\n"; } } __FILEA__ test "r5" test "r1" test "r1" version "10" test "r1" version "11" test "r1" version "12" skip "r1" version "15" test "r4" test "r4" version "10" skip "r4" version "11" test "r3" skip "r3" version "11" test "r10" skip "r10" version "11" __FILEB__ r1 r10 r5 r13
Re: Insert lines in a file
by Marshall (Canon) on Mar 16, 2014 at 16:32 UTC
    I'm not sure why you need Perl for this job?
    This looks like a spreadsheet situation.
    Do it in Excel with a few macros.