Beginner Perl Help

Soldato
Joined
27 Aug 2004
Posts
2,955
Location
Singapore ExPat
Hi Guys,

Have just started learning perl this week and it seems to be going well but I have hit a brick wall. I'm trying to do something I think should be relatively simple, just read lets say the output of the free command on linux. All I want to do is read each line into an array so I can play with the values a little.

Is an array the most sensible approach for this? Or should I use a hash? It seems that when I do this the whole lines get put into the first part of the array, lets say $array[0] whereas I want each word to do into each part of the array. I tried using split but can't seem to find the appropriate regex to split on the values.

Can anyone help?

Cheers!
 
Hi,

From what I've read the "free" command returns a table of data, please correct me if I'm wrong.

One way of storing this would be to hold each line of output in an array, and then hold all the lines together using either an array or a hash. If you held all the lines in a hash then you would be able to get at each line given a key into that line e.g Mem.

The first thing to do is to split your output into lines, try the newline character for this. This will form a list with one element for each line. Then go through this array, splitting each line into parts, either on the tab or a collection of spaces.

Here's an example of splitting a line that is space separated into an array.

Code:
my $mystring="Mem 23 54 23 12 54";
my @myarray=split(/\s/,$mystring);
foreach $b (@myarray) {print  $b;}

To split on multiple spaces use:

Code:
my @myarray=split(/\s+/,$mystring);

and to split on tabs use:

Code:
my @myarray=split(/\t/,$mystring);

When you have each line you need to insert this into either an array or a hash. You could use a hash, inserting each array for a line with the key of the first element in the array (which will be something like Mem).

Holding an array in a hash or another array is done by holding a reference to the array. And in order to get the array out of the hash you need to get it's reference and "turn" it back into an array. Do a print on an array and you'll see it's reference rather than it's contents e.g print @myarray .

Here's some code that might help explain this. The code takes a single line, splits it on multiple spaces and puts the resulting array into a hash with the key being the first element of the line. The next part retrieves the array from the hash given the key and prints out the array.

Code:
use strict;
my %myhash=();

# This is the line we are going to split.
my $mystring="Mem  23   54  23   12 54";

# Split the line on multiple spaces
my @myarray=split(/\s+/,$mystring);

# Put an entry into myhash. The key is the first element in the array,
# and the \@myarray part gets a reference to the array to put in the hash.
$myhash{$myarray[0]}=\@myarray;

# Now to get the contents out. Set $outarray to be the array reference we
# put into the hash for the key.
my $outarray =  $myhash{'Mem'};

# Now run through the array. Note, the @$outarray lets us use the
# array reference as an array.
foreach $b (@$outarray) {
        print STDOUT $b;
}

Hope that explains things a bit.

Jim
 
Thank you for that, it is beyond helpful, I am going to try this out tomorrow at work and see if I can't get this to behave as I want. Obviously this is my first week so need to get my head around the hash syntax. Hope you don't mind if I ask a couple more questions tmrw! Thanks again.

Mike
 
Hi,

No problem, glad to know the previous mail is useful, let me know how you get on. I'm happy to answer any questions, as I'm sure others are too.

Jim
 
Back
Top Bottom