Leser: 24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/perl
use Data::Dumper;
@arr = (
'/usr/bin/local',
'/usr/bin/local/fff',
'/usr/bin/locale/abc',
'/usr/bin/locale/etc',
'/usr/bino/local',
'/usr/bino/local',
'/usr/bino/local/ssss'
);
my @resultset;
my $count = 0; #Laege des laengsten elemenst des Arrays
foreach(@arr){
my @a = split('/',$_);
if($#a > $count){
$count = $#a;
}
}
foreach(@arr){
my @a = split('/',$_);
push @resultset, $resultset[0]->{$a[1]}->{$a[2]}->{$a[3]}->{$a[4]} = {};
}
print Dumper($resultset[0]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
use strict; use warnings; use Data::Dumper; sub hashify { my $path = shift; my $h = {}; my $current = $h; for (split '/', $path) { next unless length $_; $current->{$_} = {}; $current = $current->{$_}; } return $h; } print Dumper hashify('/foo/bar/baz/blubb');
1 2 3 4 5 6 7
my %x; while(@ARGV) { our $x; local $x=\%x; ()=shift=~m!/+([^/]+)(?{$x=$x->{$^N}||={}})!g; } print Dumper \%x;
/aa///bb/cc/
/dd/dd/ee/ff
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$VAR1 = { 'dd' => { 'dd' => { 'ee' => { 'ff' => {} } } }, 'aa' => { 'bb' => { 'cc' => {} } } };