in reply to i dont like the way my code looks but work

convenientstore:

In addition to the other suggestions you've already received, you can eliminate the final loop. Currently, you're making a list of matches, then printing the list at the end of your program. If you print each match as you find it, you'll no longer need the @matches array or the final loop.

...roboticus

  • Comment on Re: i dont like the way my code looks but work

Replies are listed 'Best First'.
Re^2: i dont like the way my code looks but work
by convenientstore (Pilgrim) on Jan 25, 2008 at 15:18 UTC
    thank you guys! I have modified my script
    I am trying to make my script look better and also trying to get use to put things in subroutines(because I will need to start building larger scripts
    thank you!
    use strict; use diagnostics; sub init { my %total; while (<DATA>) { #my %total; #why doesn't this work?????? next if /^#/; next if /^$/; next if ! /(server[1-4])\s+(proc[A-Z]+)\s+(proc\.[0-9]+)\s+[0- +9]+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:?[0-9]{4}?)\s+.+$/; $total{join ("\@", $3,$1)} = "$4"; } return \%total; } sub do_something { my $do_it = shift; while (<>) { chomp; my $item = $_; for (keys %{$do_it}) { print "$_ ===> $$do_it{$_}\n" if $_ eq "$item"; } } } do_something(init());
      If you declare my %total inside the loop, it will be only be visible inside the loop - and reset every time the loop starts over. Certainly not what you want.