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

Ok. Basically, somehow or another, I have an array called @sessions. This array carries three or four elements, with each element look like this:
Port User            Host/Inet/Dest   Type    Dir Status         Start   Idle
---- --------------- ---------------- ------- --- ------------- ------ ------
C0   -               -                Login   In  USERNAME           0      0
S0   aaaaaa          aaaaa            Netwrk  In  ESTABLISHED     4:08      5
S1   bbbbbb          bbbbb            Netwrk  In  ESTABLISHED     1:01      1
S2   -               -                Log/Net In  IDLE               0      0

And so on. Say the above is $sessions1. What I need to do is split $sessions1 on the newline, and then split the results of that by the columns above.

And I need to do this for each element of the array, in one big glorified loop which basically allows me to take the pretty plain text thing up there, munge it with another array of plain text thing, and spit it all out in beautiful HTML. Make sense?

(Note: I've tried playing around with getting each newline into a separate array element - I've pulled my hair out trying. Perhaps you could solve that too <g>... I'm using Net::Telnet to telnet to a terminal server, issue a command, press return 3 times, and suck all the information into an array. No matter how much I try though, I can either get all the information plopped into three array elements in the form above, or only the first screen of information plopped into individual array elements.)

Replies are listed 'Best First'.
Re: Looping through a split() and more...
by cwest (Friar) on Jun 01, 2000 at 00:45 UTC
    I hope this get's you on the right track... of course I don't have much to go on for this example :-)
    
    use CGI;
    
    my $cgi  = CGI->new;
    
    my @data = <DATA>;
    
    print $cgi->header,
          $cgi->start_html,
          $cgi->table(
                      {
                       -border      => 1,
                       -cellpadding => 3,
                       -cellspacing => 0,
                      },
                      (
                       map {
                            $cgi->Tr(
                                     (
                                      map {
                                           $cgi->td( $_ ),
                                          } split
                                     ),
                                    ),
                           } @data
                      ),
                     ),
          $cgi->end_html,
          "\n";
    
    __DATA__
    Port User            Host/Inet/Dest   Type    Dir Status         Start   Idle
    ---- --------------- ---------------- ------- --- ------------- ------ ------
    C0   -               -                Login   In  USERNAME           0      0
    S0   aaaaaa          aaaaa            Netwrk  In  ESTABLISHED     4:08      5
    S1   bbbbbb          bbbbb            Netwrk  In  ESTABLISHED     1:01      1
    S2   -               -                Log/Net In  IDLE               0      0
    
    
    The output looks like this:
    Port User Host/Inet/Dest Type Dir Status Start Idle
    ---- --------------- ---------------- ------- --- ------------- ------ ------
    C0 - - Login In USERNAME 0 0
    S0 aaaaaa aaaaa Netwrk In ESTABLISHED 4:08 5
    S1 bbbbbb bbbbb Netwrk In ESTABLISHED 1:01 1
    S2 - - Log/Net In IDLE 0 0
    --
    Casey
    
Re: Looping through a split() and more...
by swiftone (Curate) on Jun 01, 2000 at 00:02 UTC
    And I need to do this for each element of the array, in one big glorified loop which basically allows me to take the pretty plain text thing up there, munge it with another array of plain text thing, and spit it all out in beautiful HTML. Make sense?

    Not really. Could you be more specific?

    Assuming you want to break these elements into their parts, how about the following? The real question is how you want to store them. I chose an array of arrays

    foreach $sess(@sessions){ foreach(split(/\n/,$sess)){ if(/^.\d+/){ #ignore the formatting lines push @connections, [split]; #pushing a reference to the resulti +ng array onto the array } } }
    $connections[1][1]
    would have the user of the second connection, for example. See perlref
Re: Looping through a split() and more...
by lhoward (Vicar) on May 31, 2000 at 23:54 UTC
    Is this what you want???
    foreach(@sessions){ my @lines=split "\n",$_; foreach(@lines){ my ($port,$user,$host,$type,$dir,$status,$start,$idle)=split /\s+/ +,$_; # do stuff on each line here, probably want to ignore the header l +ines } }
      Don't do that with split(), (please)

      split() works as follows ( I know it may work the way you have it but.. well... that's just wrong:

      split /:/, $passwd_line;
      split '', $line; # 'Special' case
      
      Thanks,
      --
      Casey
      
        To what, in particular, are you refering?