Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Multiple Array Loops

by k_grdn (Novice)
on Oct 11, 2008 at 16:24 UTC ( [id://716616]=perlquestion: print w/replies, xml ) Need Help??

k_grdn has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

As a perl beginner I'm having problems in my programs, when storing items in array, I then grep or match theses items in another array but get mixed results due to repetitive use of for loops.

Example

my @chains = qw(INPUT OUTPUT FORWARD); my @rules = open_file($rules); foreach (@chains) { for my $rule (@rules) { print $rule if ( $_ eq $rule ); } }

I understand the results and regularly use Dumper, can someone advise better practice?

Also I can match txt between markers but have problems in the same manner using array items as string to be matched.

i.e

my FILE>> INPUT -p tcp -s 10.0.12.1 -j ACCEPT FORWARD -p udp -d 10.0.12.4 -j ACCEPT OUTPUT -p tcp -s 10.0..12.5 -j ACCEPT FILE my @chains = qw(INPUT OUTPUT FORWARD); I Loop the array open then file and try to match first array item push + on to new array until then next array item match.

Many thanks,

k_grdn

20081016 Janitored by Corion: Removed H6 tags from prose, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Multiple Array Loops
by moritz (Cardinal) on Oct 11, 2008 at 16:53 UTC
    Such nested loops can almost always be re-written to use hashes:
    my @chains = qw(INPUT OUTPUT FORWARD); # use @chains as hash keys for %chains_h; my %chains_h; @chains_h{@chains} = (1) x @chains; my @rules = open_file($rules); for (@rules) { if ($chains_h{$_}) { print "found $_"; } }
Re: Multiple Array Loops
by nobull (Friar) on Oct 12, 2008 at 16:45 UTC
    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 10.0.12.1 -j ACCEPT FORWARD -p udp -d 10.0.12.4 -j ACCEPT OUTPUT -p tcp -s 10.0..12.5 -j ACCEPT
      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

      k_grdn

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

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

        Thanks again Monks!

        k_grdn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://716616]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found