Appending Words

Posted on 2011-05-07
Last Modified: 2012-05-11

I need to append words from a file across another file by specifying columns to where append the data.


went to the store
came back from the store

If 0 is specified in a script, then it would be ideal to have input rows to be appended at the beginning of each line. If 1 is specified, then input rows would be appended after 1st word etc. In case where a row does not contain a column before or after which to append the word, it can be ignored.

Thank you
Question by:faithless1
    LVL 84

    Expert Comment

    So in this case, would you want the result for 0 to be
    Word1 went to the store
    Word2 came back from the store
    and if 1 is specified
    went Word1 to the store
    came Word2 back from the store

    LVL 26

    Accepted Solution

    This should do what you want...
    use strict;
    use warnings;
    use Fcntl 'O_RDONLY';
    use Tie::File;
    # pass in the position to place the words or default to 0
    my $loc = shift || 0;
    tie my @in, 'Tie::File', 'input.txt', mode => O_RDONLY
        or die "could not tie input.txt: $!";
    tie my @out, 'Tie::File', 'output.txt' or die "could not tie output.txt: $!";
    for my $i (0..@in-1) {
        # don't append bare words to output if it is shorter
        if ($i < @out) {
            my @words = split /\s+/, $out[$i];
            # skip if the position is longer than the line
            next if ($loc >= @words);
            splice @words, $loc, 0, $in[$i];
            $out[$i] = join ' ', @words;

    Open in new window


    Author Closing Comment

    Awesome, thank you

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now