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

How do I insert a line of text into an existing array?

#!/usr/bin/perl use strict; use warnings; my @data; my $line; my %hashTemp; open(FH, "error_log"); @data = <FH>; foreach $line (@data) { if ($line =~ /notice/) { if ($line =~ /rdy/) { $line =~ s/ /,/g; my @L1 = split(/|notice|[[]|,mpmstats:,|[\t]|rdy,|bsy,|rd, +|wr,|ka,|log,|dns,|cls,/, $line); %hashTemp = map { $_ => 1 } @L1; print @L1; } } }

I would like the following to go on the first line in the array:

day, month, date, time, year, rdy, bsy, rd, wr, ka, log, dns, cls

The output currently looks like this:

Wed,Jun,13,10:42:35,2012,769,31,0,22,6,0,0,3

Replies are listed 'Best First'.
Re: Insert Element Into Array
by frozenwithjoy (Priest) on Jun 21, 2012 at 05:51 UTC

    You can use splice. Here is an example:

    use Modern::Perl; my @nums = qw( 1 2 3 4 5 6 ); say @nums; splice( @nums, 3, 0, "combo breaker" ); say @nums;

    Output:

    123456 123combo breaker456
Re: Insert Element Into Array
by NetWallah (Canon) on Jun 21, 2012 at 05:47 UTC
    Which array would you like to insert it into ?

    Why ?

    Wouldn't it be easier to just print "day, month, date, time, year, rdy, bsy, rd, wr, ka, log, dns, cls\n" first before processing ?

    The 'unshift' function is useful in pre-pending an element to an array.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      I would like to insert it in @L1 on the first line. I can't print the string because it needs to eventually be output as a CSV file. If I were to print it within the loop it would print on every other line. I still haven't found a simple solution to this. Any ideas?

        But you are re-initializing @L1 (You use my @L1) inside the "for $line" loop.

        So, anything you add at the beginning will be obliterated by the re-initialization.

        Please re-think your logic, and reconsider my suggestion to print the titles first, before entering the loop.

        It matters little that the "print" output is redirected to a CSV file.

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Re: Insert Element Into Array
by roboticus (Chancellor) on Jun 21, 2012 at 11:05 UTC

    There are many ways you can insert data into an array:

    • push and unshift let you easily insert at the ends.
    • You can also insert at the ends with simple assignment:

      @T = ( 'FIRST', @T, 'LAST' );
    • You can also use the aforementioned splice to insert items in the middle of a list.
    • Reassigning with slices can also do the trick:

      @T = (@T[0..50], 'FOO', 'BAR', @T[51..$#T]);
    • I frequently find myself rebuilding lists into new lists as I process items:

      my @tmp; for my $item (@T) { if (..insert condition..) { push @tmp, 'Scooby Doo!'; } if (..keep condition..) { push @tmp, $item; } } @T = @tmp;

    Which method(s) you choose depend on which is most convenient in the code you're writing.

    Notes: None of these have been tested, and the ...blah.. need to be replaced with appropriate expressions.

    ...roboticus

    When your only tool is a hamster, all problems are short-lived.

    R.I.P. Hammy ~1970 .. 1972

Re: Insert Element Into Array
by cheekuperl (Monk) on Jun 21, 2012 at 05:42 UTC
    Related to this?
    Btw, you could split $line on comma and use the required fields.