Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Multiple Array Loops

by nobull (Friar)
on Oct 12, 2008 at 16:45 UTC ( #716685=note: print w/replies, xml ) Need Help??

in reply to Multiple Array Loops

I think you may have an X-Y problem here.

Guessing what your underlying problem is you probably don't want these arrays at all. You can simply look for lines that match /^[[:upper:]]+$/.

use strict; use warnings; use Data::Dumper; my %rules; { my $r = \my @uncategorised; local *_; while(<DATA>) { if ( /^[[:upper:]]+$/ ) { $r = \@{$rules{$_}}; } else { push @$r => $_; } } } print Dumper \%rules; __DATA__ INPUT -p tcp -s -j ACCEPT FORWARD -p udp -d -j ACCEPT OUTPUT -p tcp -s 10.0..12.5 -j ACCEPT

Replies are listed 'Best First'.
Re^2: Multiple Array Loops
by k_grdn (Novice) on Oct 12, 2008 at 20:16 UTC
    Thanks Both

    I have tested both pieces of code, this will aid me in development of future progs.

    Be good not to use for loops for all my progs!

    nobull do you think you could explain your code? I have gone through and would like to better understand the use of references. I am having problems dereferencing the hash looping and printing the keys and values this is just for clarity. Many Thanks


      No worries regarding the hash data, think I got it:

      for my $key ( keys %rules ) { print "$key => @{$rules{$key}}\n"; }

      Thanks again Monks!


        Hi Monks,

        I would like to thank you for your help to develop my code:

        #!/usr/bin/perl #-------------------------------------------------------------- # Script to write iptables-save syntaxed file, add rules to # relating table files mangle, nat, filter. example table file # syntax: # INPUT DROP (Chain Policy) # -p tcp -s -d --dport 143 -j ACCEPT (Rule) #-------------------------------------------------------------- use strict; use warnings; use Data::Dumper; use Tie::IxHash; #-------------------------------------------------------------- # Declare Global Vars #-------------------------------------------------------------- my $filter = "filter"; my $mangle = "mangle"; my $nat = "nat"; my @tables = ($mangle, $nat, $filter); my $date = qx(date +%d%m%y%T|sed 's/\://g'); chomp $date; my $outfile = "firewall-$date"; #-------------------------------------------------------------- # Tie to keep hash order no memory optimization #-------------------------------------------------------------- tie (my %rules, 'Tie::IxHash'); #-------------------------------------------------------------- # Open each table file, match ^uppper case, split (chain policy) # , chain as hash key, push remaining lines (rules) on array ref # to key value. Print keys:policy then values, COMMIT #-------------------------------------------------------------- sub main { open IPTSAVE, ">> $outfile" or die "Failed to open $outfile : $!"; for my $file (@tables) { %rules = (); my $r = \my @uncategorised; my ($chain,$policy); print IPTSAVE "\*$file\n"; local *_; open (FILE, "<$file") or die "Failed to open $file: $!"; while(<FILE>) { chomp; next if ( /^#|^\s^/ ); if ( /^[[:upper:]]/ ) { ($chain,$policy) = split(/ /); $r = \@{$rules{$chain}}; } else { push @$r => $_; } } close FILE; for my $key ( keys %rules ) { print IPTSAVE ":$key $policy [0:0]\n"; } for my $key ( keys %rules ) { foreach(@{$rules{$key}}) { print IPTSAVE "-A $key $_\n"; } } print IPTSAVE "COMMIT\n"; } { close IPTSAVE if eof }; } #-------------------------------------------------------------- # Run Main() #-------------------------------------------------------------- main();


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716685]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-04-16 02:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found