Storing data in perl 2D array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing data in perl 2D array
# 1  
Old 11-17-2011
Storing data in perl 2D array

Respected All,

Kindly help me out.

I have got file listings in a directory like this:
Code:
-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:
Code:
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
Code:
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
Code:
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

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-17-2011 at 11:20 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 11-17-2011
Try:
Code:
$_=`ls -lrt *stat*`
@tmp=split "\n", $_;
for ($i=0;$i<=$#tmp;$i++){
  @{$output[$i]}=(split " ", $tmp[$i])[5,6,7];
}

# 3  
Old 11-18-2011
Thanks for the solution.
Bur I need to capture the desired output in Perl 2D array and that too in a perl function, who returns that 2D array as it's return value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

2. Shell Programming and Scripting

Perl : Large amount of data put into an array

This basic code works. I have a very long list, almost 10000 lines that I am building into the array. Each line has either 2 or 3 fields as shown in the code snippit. The array elements are static (for a few reasons that out of scope of this question) the list has to be "built in". It... (5 Replies)
Discussion started by: sumguy
5 Replies

3. Shell Programming and Scripting

perl : searching for month and storing the date and time in an array

I am writing the code in perl. I have an array in perl and each variable in the array contains the data in the below format Now I need to check the below variable w.r.t system month I need to store the date and time(Tue Aug 7 03:54:12 2012) from the below data into file if contains only 'Aug'... (5 Replies)
Discussion started by: giridhar276
5 Replies

4. Shell Programming and Scripting

storing large data in unix array variable

Hi, I have table in sql ..from this table im storing the first coloumn values in shell array variable ... after this passing this variable as an arugument in SQL procedure. But the proc. is running fine only for 1024 values in array ... How to store more than 1024 values in the array... (5 Replies)
Discussion started by: ankitknit
5 Replies

5. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

6. Shell Programming and Scripting

perl-data from file save to multidimensional array

i have a file,like 1 3 4 5 6 7 8 9 i want to save it into an array. and then i want to get every element, because i want to use them to calculate. for example: i want to calculate 1 + 3. but i cannot reach my goal. open (FILE, "<", "number"); my @arr; while (<FILE>){ chomp;... (1 Reply)
Discussion started by: pp-zz
1 Replies

7. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies

8. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

9. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

10. Shell Programming and Scripting

Reading data into muti-dimentional array - in perl

Just want to learn how these are read into array but I don't seem to get it right what do I go wrong? Below is the sample Thanks input 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 #!/usr/bin/perl open (InFILE,"input"); while (<InFILE>) { @ar = split ; (5 Replies)
Discussion started by: zap
5 Replies
Login or Register to Ask a Question