Solved

Saving into multiple array?

Posted on 2003-10-27
3
199 Views
Last Modified: 2010-03-05
I have this log, which updated frequently, every new log started with date and time yyyy-mm-dd hh:mm:ss

2003-09-12 11:42:14 TRAP, SNMP v1, community public
      SNMPv2-SMI::enterprises.20022 Enterprise Specific Trap (1) Uptime: 6 days, 16:42:28.00
      SNMPv2-SMI::enterprises.20022 = STRING: " 10000 | CRITICAL | 10.55.10.3 | TEST | STATUS | PCAUSE | TEST ONLY | "
2003-09-12 11:42:17  TRAP, SNMP v1, community public
      SNMPv2-SMI::enterprises.20022 Enterprise Specific Trap (1) Uptime: 6 days, 16:42:31.04
      SNMPv2-SMI::enterprises.20022 = STRING: " 10000 | CRITICAL | 10.55.10.3 | TEST | STATUS | PCAUSE | TEST ONLY | "
2003-09-12 11:42:18  TRAP, SNMP v1, community public
      SNMPv2-SMI::enterprises.20022 Enterprise Specific Trap (1) Uptime: 6 days, 16:42:32.02
      SNMPv2-SMI::enterprises.20022 = STRING: " 10000 | CRITICAL | 10.55.10.3 | TEST | STATUS | PCAUSE | TEST ONLY | "
2003-09-12 11:42:19  TRAP, SNMP v1, community public

I want to be able to parse it and save it into multiple array automatically, i have make some script to parse it with this script:

$trapfile = "trapd.log";      
$done = 0;            
      open(IN,"<$trapfile");
      while(<IN>)
      {
               chomp;
               if(/^\s*SNMPv2\x2dSMI\x3a\x3aenterprises/)
               {
                 $done = 1;
                 $prevlog = $log;
                 s/^\s*//g;
                 $log = $_;
               }
               else
               {
                   $log = $log.$_;
               }
               if($done)
               {
                   ($string) = ($prevlog =~ /STRING\x3a\s\x22(\d+.+\x7c)\x22/g);
                   print "STRING : $string\n" if($string);
                   $done = 0;
               }
      }

        ($string) = ($log =~ /\x22\s(\d+.+\x7c\s)\x22/g);
        @itemz=split(/\s\x7c\s/,$string);

print $itemz[0];
print $itemz[1];  
print $itemz[2];  
print $itemz[3];  
print $itemz[4];
print $itemz[5];  
print $itemz[6];

but i still confused to save it in multiple array, how can I do it in perl?
0
Comment
Question by:afht
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 125 total points
ID: 9631760
Conceptually, your parsing script looks reasonable enough. I don't understand why you felt it necessary to hexify various characters in your regular expressions (and I didn't check them). It's also not quite clear to me that you need to reassemble a multi-line string when the data you are interested in is already all on one line (or are the line breaks in the STRING = component present in the trapd.log file and not just an artifact of posting here?).

By "multiple array" do you possibly mean "multi-dimensioned array"? I'm afraid I cannot visualize the data structure you would like to produce from your parsed data.

You can certainly make a slight modification to your split line and add something to keep the data in a two-dimensional array:

  my @itemz = split /\s\x7c\s/, $string;
  push @saved_data, \@itemz;

The "my" is needed so you don't overwrite previously saved data each time through the loop.

0
 
LVL 20

Expert Comment

by:jmcg
ID: 10038312
Nothing has happened on this question in over 2 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by jmcg [grade B] (progress towards, but not a complete solution).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl 101 11 88
Perl output collect 7 70
instmodsh - listing perl modules 2 95
Any syntax error for this clone.plscript 6 148
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

734 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