
BillKSmith
Veteran
Nov 18, 2011, 6:19 AM
Post #4 of 5
(2045 views)
|
|
Re: [teknokid1] Need to create a 2D array to store desired data.
[In reply to]
|
Can't Post
|
|
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
|