LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2010, 02:29 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
printing 2 arrays to 2 columns.


I have a Perl script that has two arrays - they are related.

I would like to print out the contents into two columns
next to each other.

#!/usr/bin/perl
open(PINGFILE, </home/casper/pingdata.txt") or die " can not open file ";
my @totalfile=<PINGFILE>;

foreach $string(@totalfile) {
if ($string =~ m/(^1\sping)(?=.*max\))/) {
push(usecstring,"$string");
}}
foreach $usecstring(@usecstring) {
if ($usecstring =~ m/(\d{1,20})\susec/){
push(@pingseconds, "$1 ");
}}

foreach $time(@totalfile) {
if ($time =~m/(\w{2}\:\w{2}\:\w{2}\) {
push(@pingtime, "$1 ");
}}
print scalar(@pingtime), "elements in ping array"
print scalar(@pingseconds), "elements in pingseconds array"

I tried this and a variety of join statements -
@newarray=(@pingtime, @pingdate);
foreach (@newarray) {
print "$_\n"
}

But everything I try just prints out consecutively, in one long column - one first, then the other.

I want :

$pingtime[0] $pingseconds[0]
$pingtime[1] $pingseconds[1]
$pingtime[2] $pingseconds[2]
$pingtime[3] $pingseconds[3]
$pingtime[4] $pingseconds[4]

the data structure is below

Tue Jun 8 09:36:11 EDT 2010
1 send(s) took 0 sec 39 usec (0.000039 sec avg, 0.0000039 max)
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
1 ping(s) took 0 sec 300 usec (0.000039 sec avg, 0.0000039 max)
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
Tue Jun 8 09:40:11 EDT 2010
1 send(s) took 0 sec 22 usec (0.000039 sec avg, 0.0000039 max)
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
1 ping(s) took 0 sec 301 usec (0.000039 sec avg, 0.0000039 max)
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
0 send(s) took more than 0.000050 sec
Tue Jun 8 09:42:11 EDT 2010

Last edited by casperdaghost; 06-17-2010 at 04:24 AM.
 
Old 06-16-2010, 06:03 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
First, please add code in a code block for readability.

Second, if both arrays have same number of elements, why not use a for-loop on the array index (0..$#pingtime) and then printing
the elements of both arrays with matching index?
 
Old 06-16-2010, 07:27 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Give us a sample code and desired code, that would help us to understand the problem better and give a solution faster.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing multiple columns with awk kdelover Programming 16 12-16-2009 09:10 AM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 11:27 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
printing eqivalenced arrays using gdb, g77, Linux tdr Programming 0 02-09-2004 03:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration