combining 2 lists in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • el77c
    New Member
    • Nov 2011
    • 1

    combining 2 lists in perl

    I am new to programing and am trying to learn. I am trying to use perl to merge to list of data. for example:
    list 1:
    aaaa
    bbbb
    cccc
    dddd
    list 2:
    aaaa
    bbbb
    cccc
    dddd
    The output that I want is:
    aaaa bbbb
    bbbb ccccc
    cccc dddd
    I have written a perl script and can get this type of an output:
    aaaa aaaa
    bbbb aaaa
    cccc aaaaa
    dddd aaaa

    This is my script:

    [/open FILE1, "<myfile.tx t" or die $!;
    while (<FILE1>)
    {
    my $file1_line = $_;
    chomp $file1_line;

    open FILE2, "<myfile.tx t" or die $!;
    while (<FILE2>)
    {
    my $file2_line = $_;
    chomp $file2_line;

    printf ("%s\t%s\n", $file1_line, $file2_line);

    }
    close (FILE2);
    }
    close (FILE1); ]


    I am not sure what I am doing wrong. any help would be greatly appreciated.

    thanks
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Why do you want this output?

    Code:
    aaaa bbbb
    bbbb ccccc
    cccc dddd
    What meaningful algorithm is this demonstrating, if any?

    - Miller

    Comment

    • chorny
      Recognized Expert New Member
      • Jan 2008
      • 80

      #3
      You are reading whole second file for every line of first.

      Comment

      • sathishkumar se
        New Member
        • Dec 2011
        • 10

        #4
        Hi,
        please use this program.


        open FILE1, "<myfile.tx t" or die $!;
        my $count=2;
        while (<FILE1>)
        {
        my $file1_line = $_;
        chomp $file1_line;
        open FILE2, "<myfile.tx t" or die $!;
        my $temp_count=1;
        while (<FILE2>)
        {
        my $file2_line = $_;
        chomp $file2_line;
        if($count eq $temp_count)
        {
        printf ("%s\t%s\n", $file1_line, $file2_line);
        $count++;
        last;
        }
        $temp_count++;
        }
        close (FILE2);
        }
        close (FILE1);

        Comment

        Working...