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

I've been working on a nifty little program to count up all my CodeRed attacks per day, and I realized I could chop an entire line (!!) off my program if i could change this:
@parsed=split/\s/,$line; $CR{substr($parsed[3],1,11)}[$line=~/(NNNNN)/+2*$line=~/(XXXXX)/]++;
(BTW, that takes the date, uses it as a key, and index 0 is everything else, index 1 is CRI hits on that date, and index 2 is CRII hits on that date.)
To a one liner replacing the $parsed[3], with some kind immediate access to index 3 of what split passes back, like
$CR{substr(split(/\s/,$line)[3],1,11)}[$line=~/(NNNNN)/+2*$line=~/(XXX +XX)/]++;
that example doesnt work, obviously, and i've been mangling split every which way to get that quick reference to any of it's indices. I've treated it as a real array, as an arrayref but no way i do it will get me the desired results. I've tried simple testing by just trying to print out an index like that, without it nested in substr, but regardless it doesn't work. Anyone know how to help me save a precious few bytes from my code?

Thanks

@:::::::((==========Rydor====>

Replies are listed 'Best First'.
Re: split as immediate array?
by runrig (Abbot) on Aug 11, 2001 at 19:29 UTC
    You know, sometimes it's ok to separate things for readability, but FYI, you need to put parens around the whole split call to get a slice:
    my $tmp = (split...)[3];
      That's odd. what you say works in every instance except when trying to print it immediately. Fortunately i don't need to, but that's just an odd twist. I had been trying to print it right away for my testing, which is why it didn't work, i'd say. thanks

      @:::::::((==========Rydor====>

        ...works in every instance except when trying to print it immediately.

        You're probably running afoul of the function vs. "list operator" nature of builtin's when called with and without parentheses, respectively. Specifically, perl sees that first parenthesis and says to itself, "Oh! That must be a function call," when, really, you want those parentheses to create a list that you can then subscript. So you end up subscripting the return value of print (ie, a list containing a single element: (1)) Try:

        print +(split(...))[3]; # force "list operator" mode, or... print( (split(...))[3] ); # make the whole thing a function call.

        Update: Elucidated exactly what I meant by "running afoul". :-)

        bbfu
        Seasons don't fear The Reaper.
        Nor do the wind, the sun, and the rain.
        We can be like they are.