Operating System - Linux
1752793 Members
6218 Online
108789 Solutions
New Discussion юеВ

using structure on perl scripts

 
SOLVED
Go to solution
itai weisman
Super Advisor

using structure on perl scripts

Hi everyone,
I'm trying to write a perl script using structure. (using Class::Struct)
the structure is defined as follows:
struct DiskAllocation =>
[
Disk => '$',
Fa => '@',
];

I'm using the following method to update the array (see also the function below)-
first I create another array,temporary, and then when I finish to fill it out with values, I copy the temporary array into the structure array.
I have two problems (so far...)
1. I cannot 'copy' the temporary array into the structure array, it ask me to use pointers instead. since it's a temporary array (and I want to use it afterwards), it's a problem
2. when attempting to reach data in the structure (from the 'Disk' field, which is an integer) I received the following warning (I still recieve the requested data)

Can't call method "Disk" on unblessed reference at DrpMan.pl line 219.

3. when attempting to read data from the Fa field (which is an array) I keep receving syntax errors, no matter how I try to read it.
this is the function I use to read/write the structure -

attched the section from the script, with the struct defintion and function
13 REPLIES 13
itai weisman
Super Advisor

Re: using structure on perl scripts

I managed to solve some of the problem,
but the biggest problem remains -
I can't assign an Array to structure array -
(by $Struct->new (array => @another_array
only by
$Struct->array=\@another_array)
which is not good!! since I need to clean '@another_array' from values, using refrences is not good for me...
Help me ppl!


itai weisman
Super Advisor

Re: using structure on perl scripts

I managed to solve some of the problems,
but the biggest problem remains -
I can't assign an Array to structure array -
(by $Struct->new (array => @another_array
only by
$Struct->array=\@another_array)
which is not good!! since I need to clean '@another_array' from values, using refrences is not good for me...
Help me ppl!


H.Merijn Brand (procura
Honored Contributor

Re: using structure on perl scripts

$Struct->array = [ @another_array ]

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
itai weisman
Super Advisor

Re: using structure on perl scripts

it doesn't work :(
I receive the following error -
root@troya: perl DrpMan.pl
Can't modify non-lvalue subroutine call at DrpMan.pl line 219, line 149
.
and line 219 -
$returnArray[$DiskCount]->Fa=[@Fa];
can I insert scalars values using the same syntax?
H.Merijn Brand (procura
Honored Contributor

Re: using structure on perl scripts

I don't know what that struct module does, and why you would need it.

if you have an array

my @disks = qw( /dev/dsk/c1t2d0 /media/usbdisk );

putting @disks in square brackets creates an array reference to a list of copies of the elements in @disks

lt09:/home/merijn 101 > perl -MData::Dumper -e'@x=qw(foo bar);print Dumper@x'
$VAR1 = 'foo';
$VAR2 = 'bar';
lt09:/home/merijn 102 > perl -MData::Dumper -e'@x=qw(foo bar);print Dumper\@x'
$VAR1 = [
'foo',
'bar'
];
lt09:/home/merijn 103 >

You could play with Data::Dumper and see what it expects

Still, why the need for struct?

my $ref = (
Array => [ qw( foo bar ) ],
Count => 2,
Info => {
Author => "Tux",
Company => "PROCURA",
ID => "BR41967",
Number => 42,
List => [ 1 .. 16 ],
},
Valid => 1,
);

if ($ref->{Info}{Author} eq "Tux" &&
$ref->{Array}[1] eq "bar" &&
$ref=>{Info}{List}[2] == 2) {
print "See, it's that simple!
$ref->{Extra_Info}{Age}[24] = 42;
}
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
itai weisman
Super Advisor

Re: using structure on perl scripts

thanks,
I need to design a utility to hold a lot of data on real time (not on any kind of third party database) regrading to my storage enviorment. I need to make some queires against this 'database' (we'll use that on a crisis, when we'll have to establish a DRP site ). I started writing this proggram and I came out with some complex data holders such as three dimensional arrays of hashes, I'm looking for a better way, that is easier to manage.
I didn't understand if the soultion u've wrote is an explantion of how using structure or if it's an alternative soultion.
thanks!
itai
itai weisman
Super Advisor

Re: using structure on perl scripts

what is MData::Dumper by the way? I couldn't find a module named that way...
H.Merijn Brand (procura
Honored Contributor

Re: using structure on perl scripts

Data::Dumper is a core module that shows you the real contrent of data structures. See 'man Data::Dumper' for more details.

the M comes from the command line option -M, which is a shorthand for "use Module"
see "man perlrun" for more details.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Marvin Strong
Honored Contributor

Re: using structure on perl scripts

He showed you that you don't need the struct module(whatever it does) for what you are trying to use to accomplish.

By putting your array in [ ] creates an array reference list of copies of your arrays elements.

It sounds confusing buts its really not.