Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^4: Assign Output of Regex to Array (newbie)

by justsomeguy (Novice)
on Oct 01, 2013 at 16:39 UTC ( #1056508=note: print w/replies, xml ) Need Help??

in reply to Re^3: Assign Output of Regex to Array (newbie)
in thread Assign Output of Regex to Array (newbie)

Thanks for the helpful replies. I got it to work the way I need it to, and managed to address the warnings in regards to uninitialized variables and such. Here's the code for possible feedback:

open(FILE,"<",$DATA_FILE); my @PRC_PRIV = <FILE>; my @PRC_PRIV_ONLY = grep /Run as/ .. /^ /, @PRC_PRIV; foreach $PRIV(@PRC_PRIV_ONLY) { chomp($PRIV); next if ($PRIV =~ m/Run|^ /); ($USER, $HOSTN, $CMDN) = split(" ", $PRIV, 3); if (defined($USER)) { print "$PRC_GRP,$USER,$HOSTN,$CMDN \n"; }

Any feedback welcome.

Replies are listed 'Best First'.
Re^5: Assign Output of Regex to Array (newbie)
by Laurent_R (Abbot) on Oct 01, 2013 at 18:05 UTC

    You said in the original post you wanted high performance. Try to replace this:

    my @PRC_PRIV = <FILE>; my @PRC_PRIV_ONLY = grep /Run as/ .. /^ /, @PRC_PRIV;

    with this:

    my @PRC_PRIV_ONLY = grep /Run as/ .. /^ /, <FILE>;

    And measure if you get any performance improvement.

    Another point is that you may want to include use strict; and use warnings; near the top of your program and declare your variables with the my operator. This is a recommended good practice which will save you a lot of debugging time.

      Thanks! I get about a 1ms improvement doing it that way, using "time" and after serveral iterations testing both ways. Small, but measurable.

      I left off the "use warnings" and "use strict" as well as the variable declarations for this post in interest of brevity. But I always do both.

        Just 1 ms? I thought it would be more. Against a total running time of how long?

        I left off the "use warnings" and "use strict" as well as the variable declarations for this post in interest of brevity. But I always do both.

        Fine, but then, you should rather declare your variables where you use them (in the smallest possible lexical scope), not at the top of your program. For example, you may have something like this:

        foreach my $PRIV(@PRC_PRIV_ONLY) { chomp($PRIV); next if ($PRIV =~ m/Run|^ /); my ($USER, $HOSTN, $CMDN) = split(" ", $PRIV, 3); if (defined($USER)) { print "$PRC_GRP,$USER,$HOSTN,$CMDN \n"; } }

        This way, you are defining truly local variables, and that's a good protection against silly but sometimes nasty bugs. Here above, $PRIV is scoped to the foreach loop, if you use it accidentally somewhere else in the program (which would have no sense), you'll get a compile error telling you that you are doing something wrong with it, and this is much better than having to chase the obscure reason why you don't get the output you expect. Similarly, the $USER, $HOST and $CMDN variables are scoped from the place they are declared to the end of the foreach loop. You'll be told by the compiler if you inadvertently use them where they should not be used.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1056508]
[Corion]: Hmm - nVidia GTX 1070 + Vive makes EUR 1130, leaving EUR 370 for CPU+case+cooler+ RAM - not bad
[Corion]: (I'm not a fan of AMD)
[Corion]: On the other hand, the Radeon 580 RX would cost 380 instead of 430 ... But still, that's ATi and I've been unhappy with ATi drivers the two times I had one in my desktops

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-08-21 08:35 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (319 votes). Check out past polls.