CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Need to create a 2D array to store desired data.

 



teknokid1
New User

Nov 17, 2011, 7:01 AM

Post #1 of 5 (2388 views)
Need to create a 2D array to store desired data. Can't Post

Respected All,

Kindly help me out.

I have got file listings in a directory like this:

-rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log
-rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log
-rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log
-rw-r--r-- 1 root root 115149 2011-11-20 10:15 file4.stat.log
-rw-r--r-- 1 root root 115149 2011-11-21 11:15 file5.stat.log


I am storing this output in the @data array as mentioned below:

foreach $_(`ls -lrt *stat*`)
{
@data = split " ", $_;
}


Now, I want to create a 2D array of 5x3 dimensions, Let's say output[][] in perl, to store the
desired information.This column of 2D array's output[][] will be filled by @data array's 6th,7th and 8th columns.
I want to use referances.

#
like

output[][] =
data[6] data[7] data[8]
data[6] data[7] data[8]
data[6] data[7] data[8]
data[6] data[7] data[8]
data[6] data[7] data[8]


The desired info will be

output[][] =

2011-11-17 07:15 file1.stat.log
2011-11-18 08:15 file2.stat.log
2011-11-19 09:15 file3.stat.log
2011-11-20 10:15 file4.stat.log
2011-11-21 11:15 file5.stat.log

I want to create a function for this and want to return this 2D array output[][] as the result.

Please help me out.
I shall be very thankful to you.

Warm Regards,
Teknokid1


BillKSmith
Veteran

Nov 17, 2011, 8:28 AM

Post #2 of 5 (2385 views)
Re: [teknokid1] Need to create a 2D array to store desired data. [In reply to] Can't Post

Replace @data = .... with:


Code
   

push @data, [(split)[5..7]];



This uses the default arguments for split,

Makes a reference to elements 5 through 7. Note: In perl, array index starts at zero, not one.

pushes that reference into the array @data.
Good Luck,
Bill

(This post was edited by BillKSmith on Nov 18, 2011, 5:47 AM)


teknokid1
New User

Nov 17, 2011, 9:47 PM

Post #3 of 5 (2325 views)
Re: [BillKSmith] Need to create a 2D array to store desired data. [In reply to] Can't Post

Thanks for this.
I am not getting you, Can you pls further explain, I am very new to perl.

Regards,
Teknokid1


BillKSmith
Veteran

Nov 18, 2011, 6:19 AM

Post #4 of 5 (2278 views)
Re: [teknokid1] Need to create a 2D array to store desired data. [In reply to] Can't Post


Code
use strict; 
use warnings;
my @dir_list = (
'-rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log',
'-rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log',
'-rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log',
'-rw-r--r-- 1 root root 115149 2011-11-20 10:15 file4.stat.log',
'-rw-r--r-- 1 root root 115149 2011-11-21 11:15 file5.stat.log',
);
my @data;
foreach (@dir_list) {
push @data, [(split)[5..7]];
}

foreach my $entry (@data) {
local $, = ' ';
print @$entry, "\n";
}



The function split (refer: perldoc -f split) without any arguments spits $_ on whitespace.

[5..7] is an array slice. It selects the elements (specified by the enclosed list) from the list created by split. The '..' in the list is the range operator (refer: perldoc perlop). It is a short way of specifing the list (5,6,7). (These are the elements you call 6, 7, and 8)



The square brackets around the whole thing create a reference to list of three elements.

That reference is appended to the @data array with push (refer perldoc -f push)



The next loop prints the @data array. $entry is one element from @data. It contains a reference to an array which contains the data for one directory entry.

$, (refer: perldoc perlvar) is used to format the output.

@$entry dereferences the reference.

Note: all my references to perl documentation use the tool perldoc. Type perldoc perldoc at your command line for directions on how to use it.
Good Luck,
Bill


teknokid1
New User

Nov 22, 2011, 1:02 AM

Post #5 of 5 (2046 views)
Re: [BillKSmith] Need to create a 2D array to store desired data. [In reply to] Can't Post

Hello Bill,

Thanks for the detailed guidelines.
I am extremely thankful for your valuable inputs.
Because of this, I sorted out my difficulty.I always seek your help.

Warm Regards,
Teknokid1

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives