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

Hey Everyone,
I have an array, which has 3 items. Each item looks like this:
Fri Oct 13 2000 20:11:54                                              Page  1 

The only thing that changes is the date, and time for when the script is run. I want to be able to delete everything after the time, in each item of the array, so all i have left is:

Fri Oct 13 2000 20:11:54

Thanks in advance,
Dipul Patel

Replies are listed 'Best First'.
Re: Cropping Elements in an Array
by Fastolfe (Vicar) on Nov 02, 2000 at 20:57 UTC
    If your relevant data is being held at a constant size (like most time strings of that type are), you can probably do something like this:
    $_ = substr($_, 0, 24) for @array;
    That's probably going to be more efficient than most any regexp. If you cannot guarantee that you want characters 0..24 always from this string, or that the data format will change, you should probably go with a slightly more expensive regexp alternative, as posted by others.
Re: Cropping Elements in an Array
by jptxs (Curate) on Nov 02, 2000 at 20:52 UTC

    well, let me take a crack at it:

    use strict; my $string = 'Fri Oct 13 2000 20:11:54 + Page 1'; my @array = ($string, $string, $string); for (@array) { $_ =~ s/\:(\d+)\b(\s)+(\w)+(\s)+(\d)+/:$1/; } print "$array[0]\n$array[1]\n$array[2]\n\n";
    which does:
    /home/jptxs > perl -w impala Fri Oct 13 2000 20:11:54 Fri Oct 13 2000 20:11:54 Fri Oct 13 2000 20:11:54 /home/jptxs >
    This probably isn't as robust as you want but it's a start. see perlre for some more info on what's being done. Basically, it's just a regx being applied to each member of your array and using replace to get rid of what you don't want. I can go through the regex step by step if you like, but that woudl take all the fun out of pulling it apart : )

    "sometimes when you make a request for the head you don't
    want the big, fat body...don't you go snickering."
                                             -- Nathan Torkington UoP2K a.k.a gnat

RE: Cropping Elements in an Array
by arturo (Vicar) on Nov 02, 2000 at 20:46 UTC

    Here's one way to do it, using a regex to match the HH:MM:SS timestamp that occurs at the end of the date string.

    use strict; my @dates; # read in @array, then .. foreach (@array) { # there is no doubt a more stylish way to do this, but ... #regex matches the part of $_ up to the point it finds the pattern # two digits followed by a colon (etc.) /(^.*?\d\d:\d\d:\d\d)/; # pushes the part of the string matched into the dates array push @dates, $1; }

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

(tye)Re: Cropping Elements in an Array
by tye (Sage) on Nov 02, 2000 at 21:49 UTC
    s/\s{10}.*// for @array;

    Removes the first break of 10 or more spaces (or other white space characters) and any characters after that.

            - tye (but my friends call me "Tye")
RE: Cropping Elements in an Array
by agoth (Chaplain) on Nov 02, 2000 at 20:51 UTC
    Hideously untested but you should get the idea:
    for (@yourarray) { chomp; # maybe my @ary = split / /, $_; for (0..1) { pop @ary; } print "@ary\n"; }

      Go one step further:
      for (@yourarray) { my @ary = (split/ /, $_, 5)[0 .. 4]; }
      or even the also untested:

      @yourarray = map { join(" ", (split(/ /, $_))[0 .. 4]) } @yourarray;


        I was trying to avoid the slice of the first few, I don't know why ;-), I fancied lopping two items off the end.
        too lazy to use splice......
        splice(@ary, -2);

Re: Cropping Elements in an Array
by ImpalaSS (Monk) on Nov 02, 2000 at 22:08 UTC
    Thanks Guys, Once Again :)
    Dipul