Perl Foreach through a multidimensional array?

I have a script that takes files / files that are too old (as defined through user input) and deletes them. I want to list each directory separately and ask for confirmation to delete old files in each directory.

So, I have an array (@oldDirs) that contains several arrays (each directory) that store strings (full path names).

@oldDirs = (@AirDefense,@Arcsight,@Avocent,@BlueCoat,@Checkpoint,@Cisco,@Crossbeam,@FireE ye,@lostAndFound,@rancid,@Riverbed,@Symantec,@Wiki,@WLAN) if($oldPathsSize != 0) { my $dirNamesCounter = 0; my @dirNames = ('/AirDefense','/Arcsight','/Avocent','/BlueCoat','/Checkpoint','/Cisco','Crossbeam','FireE ye','/lost+found','/rancid-2.3.8','/Riverbed','/Symantec','/Wiki','/WLAN'); foreach my @direc (@oldDirs) { my $size = @direc; print "Confirm Deletion of $size Old files in $dirNames[$dirNamesC]: \n"; $dirNamesCounter++; foreach my $pathname (@direc) { print "\t$pathname\n"; } print "\nDelete All?(y/n): "; my $delete = <STDIN>; chomp($delete); if($delete eq "y") { #delete file } } } 

My problem is with the first foreach statement. Both arrays and @direc are not allowed, because I need to have a scalar value in this part of foreach ... but they are all arrays! How should I do this, if possible?

+4
source share
1 answer

You should read perllol - Manipulating Array Arrays in Perl . You cannot create a multidimensional array as follows:

 my @array1 = (1, 2, 3); my @array2 = qw/abc/; my @multi = (@array1, @array2); 

Instead, the arrays will be flattened, and @multi will contain

 1, 2, 3, 'a', 'b', 'c' 

You should use the links:

 my @multi = (\@array1, \@array2); for my $ref (@multi) { for my $inner (@$ref) { # ... } } 
+15
source

Source: https://habr.com/ru/post/1484722/


All Articles