Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: splice an array

by LanX (Saint)
on May 03, 2011 at 22:06 UTC ( [id://902771]=note: print w/replies, xml ) Need Help??


in reply to splice an array

(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

Replies are listed 'Best First'.
Re^2: splice an array
by dbs (Sexton) on May 04, 2011 at 12:52 UTC
    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 " s«»«»g " Substitution replacement not terminated at -e line 1. $ perl -MO=Deparse -e " s«««g " s///g; -e syntax OK
      s'''g is special in that it doesn't interpolate
        I am not getting this solution guys. I appreciate the help, but had a hard time understanding Rolfs code and played with it for a while with little success. With that being said, my temp solution, I understand is the way I would like to follow.
        while ( defined( $_ = <$Rlog> )) { s/^Alert Level\s0*(?:[3-9]|\d{2,})/DEREK_SMITH\n$&/s; } continue { print $_; } ###-- this works, but only half way. __OUT__ ========================================== 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 DEREK_SMITH 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 DEREK_SMITH Alert Level 5: TEST ALERT!!!!!!!!!!!!!!!!!!!! Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D5787300200C0 FFFF027000120300 __DATA__NEEDED__ ========================================== 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 DEREK Alert Level 4: TEST ALERT!!!!!!!!!!!!!!!! SMITH 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 DEREK_ Alert Level 5: TEST ALERT!!!!!!!!!!!!!!!!!!!! Keyword: Type-02 127002 1208322 SMITH Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D5787300200C0 FFFF027000120300
        either SMITH after 'Keyword' or after 'Alert' either one is fine.
        I got this working better that halfway, and its usable. Any other help is welcome!
        while ( defined( $_ = <$Rlog> )) { s/^Alert Level\s0*(?:[3-9]|\d{2,})/DEREK\n$&\nSMITH\n/s; } continue { print $_; } __OUT__ ========================================== Log Entry 11: 13 Feb 2011 07:24:32 DEREK Alert Level 5 SMITH : TEST ALERT!!!!!!!!!!!!!!!!!!!! Keyword: Type-02 127002 1208322 Soft Reset Logged by: Baseboard Management Controller; Sensor: System Event 0x204D5787300200C0 FFFF027000120300 ==========================================
      $ 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://902771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found