[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Split help




----------
>From: bobbys <bobbys@i-2000.com>
>To: macperl@macperl.org
>Subject: [MacPerl] Split help
>Date: Tue, Nov 16, 1999, 7:57 AM
>

>Hi there-
>
>I'm having quite a bit of problem getting the "split" function to work the way
>I'd like it to.
>
>Here's the scenario: (in case anybody would like to pint out a better way to
>accomplish this)
>I'm reading in the contents of a text file.
>I'm looking for a specific phrase in this content ("FOUND ICONS:\n")
>I'm looking to extract all the data that comes AFTER this phrase (read it into a
>newline delimited array, remove dups, and then...)
>Add new data into said file.
>
>Here's how I'm tackling this:
>
>open (SPLITTEST, "icekake:Desktop Folder:testCase") || die "can't open file:
>$!";
>while (<SPLITTEST>){
>@splits= split (/"FOUND ICONS:\n"/, <SPLITTEST>);
>print STDOUT $splits[1]; #debug code... I want to see the data
>}
>
>I'm expecting to get everything listed AFTER "FOUND ICONS:\n". Instead, I get a
>listing of every other line from the entire file!?
>
>Can someone explain what I'm doing wrong?

Sure.  The problem is that you are using <SPLITTEST> inside the split
statement, which makes split loop through each line of "SPLITTEST."  Every
iteration of the loop, the while loop reads a line from <SPLITTEST> and then
your split statement reads the next line.  It never finds the pattern "FOUND
ICONS:\n" in the line it reads, so it outputs the whole line.  Here's how I
would do it:

undef(@splits);
$flag = 0;
while(<SPLITTEST>) {
    if(/FOUND ICONS:\n/) {
        $flag = 1;
    }
    if($flag) {
        push(@splits, $_);
    }
}

This will give you an array containing each of the lines after the "FOUND
ICONS:."  Each of these lines will be terminated by a newline. (which is
what I think you mean by a "newline-terminated array")  If you need to put
the lines back together into one big string, of course, you can use the join
command.

Hope this helps,

Mike Nichols
mike.nichols@mail.utexas.edu

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org