Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

splice an array

by dbs (Sexton)
on May 03, 2011 at 18:31 UTC ( #902755=perlquestion: print w/replies, xml ) Need Help??

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

Hi All!

GOAL: insert new text before at a minimum and after a block of text ideally.

I put data in a file like so:

while (<$RLOG>) { if (/(^Log Entry\s\d+:.*)/) { ++$recordcount; my $N = $1 .+ "\n\n"; my $e = <$RLOG>; my $x = <$RLOG>; my $t = <$RLOG>; my $l = <$RLOG>; my $n = <$RLOG>; my $s = <$RLOG> .+ "\n======================================== +==\n"; push @ary, ($N,$e,$x,$t,$l,$n,$s); } } for (@ary) { print $PLOG, $_; }
I am able to find any text entry "Alert Level 3" or higher, but then, I want to insert rtf code before this block of text and at the end of this block of text. I am failing on the insert before and insert after.
while(<$PLOG>) { ++$alert3orMoreCount if /^Alert Level\s0*(?:[3-9]|\d{2,})/i; $pos = $. if /^Alert Level\s0*(?:[3-9]|\d{2,})/i; }
I tried using splice like so:  splice( @ary, $pos, 0, $RTF{ctbl}, $RTF{red}, $RTF{ered} );

It seems silly to write the array data to a file, then try and manipulate it after. Below is the raw data file. Thank you!

__DATA__ Log Entry 14: 27 Feb 2011 10:03:42 Alert Level 2: Informational Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D6A217E0200F0 FFFF027000120300 ========================================== Log Entry 13: 20 Feb 2011 08:33:42 Alert Level 2: Informational Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D60D1E60200E0 FFFF027000120300 ========================================== Log Entry 12: 20 Feb 2011 07:14:27 Alert Level 4: TEST ALERT!!!!!!!!!!!!!!!! Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D60BF530200D0 FFFF027000120300 ========================================== Log Entry 11: 13 Feb 2011 07:24:32 Alert Level 5: TEST ALERT!!!!!!!!!!!!!!!!!!!! Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D5787300200C0 FFFF027000120300 ========================================== Log Entry 10: 06 Feb 2011 10:36:26 Alert Level 2: Informational Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D4E79AA0200B0 FFFF027000120300 ==========================================

Replies are listed 'Best First'.
Re: splice an array
by umasuresh (Hermit) on May 03, 2011 at 21:50 UTC
    It may be visually useful to indicate where exactly you want to insert text, something like:
    Log Entry 10: 06 Feb 2011 10:36:26 ---> Alert Level 2: Informational ---> Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D4E79AA0200B0 FFFF027000120300
    Is the arrow heads where you want to insert the text?
    UPDATE: got it, I misunderstood the question initially!
      Here is a partial solution inspired by an earlier post with very little modification!
      Assuming the input record separator is a "=====":
      use strict; use Data::Dumper; my @seq; # array of sequences my %seq; # hash lookup of sequences based on id's { local $/ = '=========================================='; while (<DATA>) { s/^==========================================//g; # strip out '>' +from beginning s/==========================================$//g; # and end of line my $line = $_; next if !length($_); # ignore empty lines next if /^\n$/; my @a = split /\n/, $_; # collect lines in an array my @new_a = grep {$_ ne "" } @a ; # remove empty lines #s/^(.*)\n//; # and strip it out my @rec; s/\n/\t/mg; # join the sequence strings push @rec, @new_a; # push sequence into an array push @seq, \@rec; # push into array of sequences push @{ $seq{$new_a[1]} }, \@rec; # or store in a hash loo +kup } } print Dumper(\@seq); print Dumper(\%seq); __DATA__ ========================================== Log Entry 14: 27 Feb 2011 10:03:42 Alert Level 2: Informational Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D6A217E0200F0 FFFF027000120300 ========================================== Log Entry 13: 20 Feb 2011 08:33:42 Alert Level 2: Informational Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D60D1E60200E0 FFFF027000120300 ========================================== Log Entry 12: 20 Feb 2011 07:14:27 Alert Level 4: TEST ALERT!!!!!!!!!!!!!!!! Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D60BF530200D0 FFFF027000120300 ========================================== Log Entry 11: 13 Feb 2011 07:24:32 Alert Level 5: TEST ALERT!!!!!!!!!!!!!!!!!!!! Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D5787300200C0 FFFF027000120300 ========================================== Log Entry 10: 06 Feb 2011 10:36:26 Alert Level 2: Informational Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D4E79AA0200B0 FFFF027000120300 ==========================================
      Once you have it in a data structure as shown in the output, you can modify the code to insert text before and after based on the required specification.
Re: splice an array
by LanX (Cardinal) on May 03, 2011 at 22:06 UTC
    (I'm not sure if I really understood your problem...)

    what about creating a new array instead of slicing?

    for (@old) { push @new,@pre if /START_RE/; push @new,$_; push @new,@post if /END_RE/; }

    (untested)

    HTH

    UPDATE:

    Generally it's better to use an iterator that returns separated records.

    Have a look at the "alternativ iterator" Re^2: reading several lines in a gulp (iterator) and change the last condition to split at /^=====================/

    Cheers Rolf

      thanks guys! I was/am trying this too:

      LINE: while ( defined( $_ = <$Rlog> )) { s/^Alert Level\s0*(?:[3-9]|\d{2,})/(?!.*Log Entry)/DEREK!!!!\n$&/s +; } continue { print $Plog $_; }

      but keep getting this error:

      Bareword found where operator expected at foo line 12, near "s/^Alert +Level\s0*(?:[3-9]|\d{2,})/(?!.*Log Entry)/DEREK!!!!" Backslash found where operator expected at foo line 12, near "DEREK!!! +!\" syntax error at foo line 12, near "s/^Alert Level\s0*(?:[3-9]|\d{2,})/ +(?!.*Log Entry)/DEREK!!!!!" Execution of foo aborted due to compilation errors.
      ORDER OF print new code where arrows are.
      Log Entry 10: 06 Feb 2011 10:36:26 ---> Alert Level 2: Informational ---> Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D4E79AA0200B0 FFFF027000120300
        You have one too many slashes in your s/// (you have 4, but you can only use 3). Either backslash one of the 2 in the middle, or choose different delimiters:
        s/^Alert Level\s0*(?:[3-9]|\d{2,})/(?!.*Log Entry)/DEREK!!!!\n$&/s; ^ ^ ^ ^ | | | | | | | |
        I second toolics suggestion of using alternate delimiters. These are all equivalent
        perl -MO=Deparse -e " s///g " perl -MO=Deparse -e " s\\\g " perl -MO=Deparse -e " s[][]g " perl -MO=Deparse -e " s()()g " perl -MO=Deparse -e " s{}{}g " perl -MO=Deparse -e " s!!!g " perl -MO=Deparse -e " s###g " perl -MO=Deparse -e " s vvvg " perl -MO=Deparse -e " s ___g " perl -MO=Deparse -e " s {}//g " perl -MO=Deparse -e " s {}\\g " perl -MO=Deparse -e " s {}vvg " perl -MO=Deparse -e " s {}()g " perl -MO=Deparse -e " s {}[]g " perl -MO=Deparse -e " s {}<>g " perl -MO=Deparse -e " s<><>g "
        They all output
        s///g; -e syntax OK
        But don't get fooled by the balancing act, it doesn't work for other chars like and
        $ perl -MO=Deparse -e " sg " Substitution replacement not terminated at -e line 1. $ perl -MO=Deparse -e " sg " s///g; -e syntax OK
        s'''g is special in that it doesn't interpolate
        $ perl -e " s/// " $ perl -e " s//// " syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors. $ perl -e " s///blah;/ " Bareword found where operator expected at -e line 1, near "s///blah" syntax error at -e line 1, near "s///blah" Search pattern not terminated at -e line 1.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://902755]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2021-04-23 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?