data structure

M

mxa

Hi,
I need to set up a data structure that is very similar to two level
unix directory.
my ( @files, %dir )
@files=('file1','file2','file3');
$dir{'mydir'} = ( @files)
$dir{'root'} = ( $dir{'mydir'})

so root has many directories ( no files ) and each directory has many
files.

I am having problem accessing the above structure , any suggestion
is greatly appreciated ( please note that the actual problem has
nothing to do with dir and files ).

thanks
Michael
 
W

William Goedicke

Dear mxa -

mxa> Hi, I need to set up a data structure that is very similar to
mxa> two level unix directory. my ( @files, %dir )
mxa> @files=('file1','file2','file3'); $dir{'mydir'} = ( @files)
mxa> $dir{'root'} = ( $dir{'mydir'})

mxa> so root has many directories ( no files ) and each directory
mxa> has many files.

I would use something (untested) like:

my %root;
$root = ( 'd1' => [ 'd1f1', 'd1f2', 'd1f3' ],
'd2' => [ 'd2f1', 'd2f2' ],
'dn' => [ 'dnf1', 'dnf2' ,'dnfn' ],
);

then the following should work

foreach my $aref $root{'d1'} {
foreach @{$sref} {
print;
}
}

Yours - Billy

============================================================
William Goedicke (e-mail address removed)
Cell 617-510-7244 http://www.goedsole.com:8080
============================================================

Lest we forget:

Good management involves five activities:

o Facilitate your staff's efforts by removing organizational
impediments.

o Identify each staff member's strengths and goals
and veer their responsibilities towards them.

o Assign responsibility with commensurate authority.

o Set realistic expectations in your customers.

o Evangelize your services.

- William Goedicke
 
P

Paul Lalli

I need to set up a data structure that is very similar to two level
unix directory.
my ( @files, %dir )
@files=('file1','file2','file3');
$dir{'mydir'} = ( @files)
$dir{'root'} = ( $dir{'mydir'})

so root has many directories ( no files ) and each directory has many
files.

I am having problem accessing the above structure , any suggestion
is greatly appreciated ( please note that the actual problem has
nothing to do with dir and files ).

You can't assign an array to a scalar variable. You can only assign
array references to a scalar variable.

I strongly suggest you read up on references and multi-dimension data
structures:
perldoc perlreftut
perldoc perllol
perldoc perldsc
perldoc perlref

Any and all of those will be helpful to you.

Paul Lalli
 
M

mxa

Hi,

beautiful , it worked :

my %root;
%root = ( 'd1' => [ 'd1f1', 'd1f2', 'd1f3' ],
'd2' => [ 'd2f1', 'd2f2' ],
'dn' => [ 'dnf1', 'dnf2' ,'dnfn' ],
);
p Dumper(\%root)
$VAR1 = {
'dn' => [
'dnf1',
'dnf2',
'dnfn'
],
'd1' => [
'd1f1',
'd1f2',
'd1f3'
],
'd2' => [
'd2f1',
'd2f2'
]
};

thanks
Michael
 
S

Steven_Smith

Hi,
I need to set up a data structure that is very similar to two level
unix directory.
my ( @files, %dir )
@files=('file1','file2','file3');
$dir{'mydir'} = ( @files)
$dir{'root'} = ( $dir{'mydir'})
I use a recursive hash that looks like
my %dirs = (
dirs => {},
files => [],
);

Each dirs reference will then point at another dirs
hash, so to go a bit deeper:

%dirs = (
dirs => { dirs => { dirs => { ...},
files => [...],
},
files => [ ... ],
},
files => [ ... ],
);

Things get a bit more complicated when you start throwing
in links, but for most of what I do, I ignore them.

Steve S.
Steve S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top