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

OK well theres probably something I'm missing not that it doesnt work.

I am trying to split strings that look like this - "Wed 24 May 2006 09:56:12 AM EDT kdelibs3-devel"

I used @info = split(/ /, $reversedpackages[$index]); but the last element in info is "EDT kdelibs3-devel" instead of being "kdelibs3-devel".

I got @reversedarray using rpm -qa --queryformat '%{INSTALLTIME}\t%{INSTALLTIME:date}\t%{NAME}\n' |            sort -g | cut -f 2-

What am I doing wrong ?

Edited by planetscape - added code tags

( keep:0 edit:12 reap:0 )

Replies are listed 'Best First'.
Re: Split not working
by bpphillips (Friar) on Jun 06, 2006 at 15:59 UTC
    You're splitting on spaces but in your --queryformat argument to rpm you're specifying that the INSTALLTIME:date and NAME fields should be seperated by \t (a tab character). Try:
    split(/\t/, $data);
Re: Split not working
by swampyankee (Parson) on Jun 06, 2006 at 16:15 UTC

    Whenever you say "X is not working," please tell us what you mean by "working." A log function is "working" if it throws an exception for log(0) but may not be "working" if it throws an exception for log(-1), which is defined in the complex domain.

    Would I be correct in concluding that you want to split your string on white space? If that's the case try this:

    $string="Wed 24 May 2006 09:56:12 AM EDT kdelibs3-devel"; @info = split(/\s+/, $string);

    \s is the regex abbreviation for whitespace, which is the class of characters including blanks and tabs. Your surprise may be that what you expect to be the last field is separated from what you expect to be the penultimate field by a tab, vs a blank. To check this you could open the file in vi (or vim) and :set list, which will indicate tabs.

    As an aside, please use code tags to surround your code and use html entities for square braces([ ]). These will make it much easier to read your posts.


    e(π√−1) = −1
Re: Split not working
by socketdave (Curate) on Jun 06, 2006 at 16:06 UTC
    Good advice from my fellow monks... But if you want to get the other values into your @info array individually, you want to split on \s (any whitespace) instead of \t (tabs).
Re: Split not working
by ambrus (Abbot) on Jun 06, 2006 at 17:56 UTC

    Note that splitting on / / is different from splitting on " ". The latter splits on any sequence of whitespace and also throws away trailing whitespace. (Splitting on /\s+/ is akin to splitting on " " but gives an empty field if the string starts with whitespace. You can also do /\S+/g instead of split " ".)

Re: Split not working
by ptum (Priest) on Jun 06, 2006 at 16:02 UTC

    Well, on the surface, it looks to me as though you are trying to split by a literal space, yet your string is really tab-delimited. Try splitting on \t instead, or, better yet, try posting your actual code (in <code> blocks, of course) so we can help you more effectively.

    Update: Curses! Pre-empted by a speedier monk yet again! :)

    No good deed goes unpunished. -- (attributed to) Oscar Wilde