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: Beginner:
Storing an entire array into one value of another array

 



l4teral
User

May 25, 2010, 1:28 PM

Post #1 of 15 (1146 views)
Storing an entire array into one value of another array Can't Post

I have a script I'm writing, where it outputs the returned data from a telnet session. Currently, I have it storing the output into one array, which works fine. I want to make a history of the last 100 times that the command was issued (the data it gives back is relatively small, so 100 really isn't that large..) and store it somehow, without a file.

What comes to mind is a two dimensional array, however I cant seem to find anything with this situation. Any help would be greatly appreciated. :)

here's some pseudo code to get the jist of what i'm trying to do:

Code
@lines = $t->cmd("some command"); 
@history[$a] = @lines;
$a++;

while ($x < 39)
{
print "$lines[$x]\n";
$x++;
}


So it first takes the entire array of @lines, and tries to store it into one slot of @history


FishMonger
Veteran / Moderator

May 25, 2010, 1:39 PM

Post #2 of 15 (1140 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post

You need to store it as a reference.

Change:

Code
@history[$a] = @lines;


To:

Code
@history[$a] = \@lines;



l4teral
User

May 25, 2010, 1:52 PM

Post #3 of 15 (1135 views)
Re: [FishMonger] Storing an entire array into one value of another array [In reply to] Can't Post

Thanks for the quick reply.


When I do this, and modify the code:

Code
@lines = $t->cmd("devstat"); 
@history[$a] = \@lines;
print @history[$a];


It outputs

Code
ARRAY(0x1b07e6c)

...I don't understand what exactly just happened :shrug:


l4teral
User

May 25, 2010, 1:56 PM

Post #4 of 15 (1133 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post

I'll clarify that every time the script passes through the loop (fairly frequently), @lines array gets overwritten. (Spawning the need for a @history array to be able to look at [x] past @lines histories.)


FishMonger
Veteran / Moderator

May 25, 2010, 7:38 PM

Post #5 of 15 (1116 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post

You need to dereference the array ref.


Code
use strict;  
use warnings;

my @lines = qw(a b c);
my @history;

$history[0] = \@lines;

print $history[0]->[1];



l4teral
User

May 26, 2010, 9:51 AM

Post #6 of 15 (1107 views)
Re: [FishMonger] Storing an entire array into one value of another array [In reply to] Can't Post

This doesn't seem to change anything. I temporarily got around it, by feeding the entire array into a variable....but this is not desirable, as I have less control over sifting through the data later on. =(


FishMonger
Veteran / Moderator

May 26, 2010, 10:11 AM

Post #7 of 15 (1106 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post

Can you post a more complete sample of your code?

Have you tried using Data::Dumper dump the @history array so you could see its structure?


l4teral
User

May 26, 2010, 11:06 AM

Post #8 of 15 (1103 views)
Re: [FishMonger] Storing an entire array into one value of another array [In reply to] Can't Post

well, to debug I added a line to print out the entire @lines array.....it shows up just fine. This is the only section of the script that is referencing the two arrays, so I can't imagine anything else could be conflicting... Frown


FishMonger
Veteran / Moderator

May 26, 2010, 11:18 AM

Post #9 of 15 (1101 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post

Based on the fact that yesterday you were receiving ARRAY(0x1b07e6c) as the output, that clearly indicates that you're not dereferencing the array correctly. The best way to see the structure of the array is to use Data::Dumper dump it out.


FishMonger
Veteran / Moderator

May 26, 2010, 11:22 AM

Post #10 of 15 (1100 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post

Hmm,

I just noticed a copy/paste error on my part, which maybe throwing you off.


Code
@history[$a] = \@lines;


should have been


Code
$history[$a] = \@lines;



l4teral
User

May 27, 2010, 7:07 AM

Post #11 of 15 (1093 views)
Re: [FishMonger] Storing an entire array into one value of another array [In reply to] Can't Post

pardon me for asking...but I can't seem to find a clear cut way of using data dumper that doesn't have some complex script doing 100 other things. Can I get a snippet of code or a link to a good help tutorial in dumping it?


l4teral
User

May 27, 2010, 7:27 AM

Post #12 of 15 (1090 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post

Let's take a different approach here -


- Let's say I wanted to save the ENTIRE contents of an array into one big variable? Is this possible?


FishMonger
Veteran / Moderator

May 27, 2010, 7:49 AM

Post #13 of 15 (1089 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post


Code
use strict; 
use warnings;
use Data::Dumper;

my @history;

for (1..3) {

# random numbers to simulate your telnet command
my @lines = (int(rand(100)), int(rand(25)), int(rand(50)) );

push @history, \@lines;
}

# dump the entire data structure
print Dumper @history;

# print a single element
print $history[1]->[2];


http://perldoc.perl.org/perldsc.html#Declaration-of-an-ARRAY-OF-ARRAYS


FishMonger
Veteran / Moderator

May 27, 2010, 7:56 AM

Post #14 of 15 (1088 views)
Re: [l4teral] Storing an entire array into one value of another array [In reply to] Can't Post


Code
$bigvar .= join('', @lines);



l4teral
User

May 28, 2010, 12:17 PM

Post #15 of 15 (1059 views)
Re: [FishMonger] Storing an entire array into one value of another array [In reply to] Can't Post

$bigvar .= join('', @lines); worked perfectly.......thank you thank you thank you!

I adjusted it into my code, and it worked like a charm.

Code
@lines = $t->cmd("somecommand"); 
$history[$slot] .= join('', @lines);



now, I just need to figure out how to manage the size of that @history array...

 
 


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

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