开发者

splitting an array into variables

I am weak when it comes to splitting the fields of an array (array[0] - array[6])wal into varialbles in perl

This is the end of a large program - the results get pumped in to an array, and then emailed in a tabular format - the array that gets pumped out looks like the static @mailarray below. The table gets formatted ok in the email, it just pumps out the same line - for each line of the array. It needs to traverse the whole array 7 fields to each line.

mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2

The end of the program looks like this:

#!/usr/bin/perl

@mailarray = qw(mrktsrvr ltick Chicago tkscan100 tmscn Brooklyn OCOMsg2
mrktsrvr ltick Chicago ctdb811 dltic080 Brooklyn OCOMsg2
mrktsrvr ltick Chicago farm101 bhsrv0 Brooklyn OCOMsg2
mrktsrvr ltick Chicago farm102 bhsrv0 Brooklyn OCOMsg2
mrktsrvr ltick Chicago rnds110 nictsrv1 Brookly开发者_Go百科n OCOMsg2
mrktsrvr ltick Chicago relay11 tryticUS1 Brooklyn OCOMsg2);
($header_host, $header_process, $header_host_region, $stat_host, $stat_process, $stat_region, $stat_message_type)=@mailarray;
print $header_host;
print "@mailarray\n";
if (@mailarray){
  #open(MAIL,"| /usr/sbin/sendmail -t") || warn "Can't send email\n";
  print  "to:casper\@mail\n";
  print  "from: $header_host\n";
  print  "Subject: ping_stat for $header_host $yyyymmdd\n";
  print  "Content-type: text/html\n\n";
  print  "<html><body><center><b>ping_stat messaging violations for $header_host $yyyymmdd</b>\n";
  print  "<table border=1><tr><td>Process</td><td>Acceptable Regions</td><td>Violation of Region</td><td>Message Type</td></tr>\n";
  foreach (@mailarray){
        ($header_host, $header_process, $header_host_region, $stat_host, $stat_process, $stat_region, $stat_message_type)=@mailarray;
#split(/\s+/, $_);
        print  "<tr><td>$header_process" . ":" . "$header_process</td><td>$header_host_region</td><td>$stat_host" .":". "$stat_process" . "-" . "$stat_region</td><td>$stat_message_type</td></tr>\n";
  }
  print  "</table>\n";
  print  "<br><br>\n";
  print "</table></center></body></html>";
  close ;
}


If I understand correctly, your problem is that you don't know how to split those strings - this is doing it:

use strict;
use warnings;
use Data::Dumper;

my $str = <<END
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
mrktsrvr:ltick Chicago tkscan100:tmscn-Brooklyn OCOMsg2
END
;

foreach my $line ( split( /\n/, $str ) ){
    my @array = split( /\W/, $line);
    print Dumper(\@array);
}


You should have used an array of arrays.

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

my @mailarray = (
    [qw/ mrktsrvr ltick Chicago tkscan100 tmscn Brooklyn OCOMsg2 /],
    [qw/ mrktsrvr ltick Chicago ctdb811 dltic080 Brooklyn OCOMsg2 /],
    [qw/ mrktsrvr ltick Chicago farm101 bhsrv0 Brooklyn OCOMsg2 /],
    [qw/ mrktsrvr ltick Chicago farm102 bhsrv0 Brooklyn OCOMsg2 /],
    [qw/ mrktsrvr ltick Chicago rnds110 nictsrv1 Brooklyn OCOMsg2 /],
    [qw/ mrktsrvr ltick Chicago relay11 tryticUS1 Brooklyn OCOMsg2 /],
);

for (@mailarray) {
    print Dumper \@$_;
}


I'm not sure if i understand your question, but if you want to know a way how to extract seven array fields to variables, this should work:

for(my $i = 0; $i < @mailarray; $i += 7){
 my ($header_host, $header_process, $header_host_region, 
     $stat_host, $stat_process, $stat_region, 
     $stat_message_type) = @mailarray[$i..$i+7];
     print "&lt;tr&gt;&lt;td&gt;$header_process:" .
       "$header_process&lt;/td&gt;&lt;td&gt;".
       "$header_host_region&lt;/td&gt;&lt;td&gt;$stat_host".
       ":$stat_process"."-"."$stat_region&lt;/td&gt;&lt;td&gt;".
       "$stat_message_type&lt;/td&gt;&lt;/tr&gt;\n"
}

btw: think of

  1. HERE-Docs using them makes it a lot of easier to write literal multiline strings (you can see them in a previous post)

  2. … using a better data structure e.g. a List of Hashes

  3. … formatting your code properly, your string literals are already divided with dots, why not putting a newline there?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜