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

I have a HUGE string with ";" as the delimiter between items within the string, how do I extract each one? perhaps swop the delimiter with a <CR> and then shove into an array? Below is my feeble attempt...
open (IF, "<$thefile"); $rawstring = (<IF>); close (IF); while (length($rawstring) > 0) { ($item, $rawstring) =~ split/\;/,$rawstring; print "$item\n"; }

Replies are listed 'Best First'.
Re: Split long string MANY times?
by japhy (Canon) on Feb 13, 2001 at 20:46 UTC
    I sense you don't want to build a huge nasty list. Well, you can use split for that:
    while (length $source) { # make only one split (creating 2 parts) (my($rec),$source) = split /;/, $source, 2; # ... }
    Or you could use a regex. That has the benefit of not removing content from $source.
    while (my ($rec) = $source =~ /\G([^;]*)(?:;|$)/g) { # ... }
    Or, if you're a major fan of C, I offer you strtok() in Perl:
    use constant RE_MAGIC => bless [], 'RE_MAGIC'; use constant C_STYLE => bless [], 'C_STYLE'; { my ($re,$str); sub strtok { my ($s,$pat,$opt) = @_; if (defined $s) { local $^W; # in case $pat/$opt isn't a ref $re = ref($pat) eq "RE_MAGIC" ? qr/(?!\s*$)\s*(\S*)/ : $opt && ref($opt) eq "C_STYLE" ? qr/(?!(?:$pat)*$)((?:(?!$pat).)*)(?:(?:$pat)+|$)/ : qr/(?!(?:$pat)*$)((?:(?!$pat).)*)(?:(?:$pat)|$)/; $str = $s; } else { croak "no pattern for strtok()" if not defined $re; } $str =~ /\G$re/g and return $1; undef $re; undef $str; } } my $rec = strtok($source, ';'); while (defined $rec) { # ... $rec = strtok(); }
    There are two special flags to send to this function -- RE_MAGIC is sent in place of the pattern, and signifies Perl's special split(' ', ...) behavior; C_STYLE splits on multiple occurrences of tokens (so that "a--b" split on "-" will return a and b, and no empty string).

    japhy -- Perl and Regex Hacker
Re: Split long string MANY times?
by arturo (Vicar) on Feb 13, 2001 at 20:35 UTC

    If I understand your question correctly, there's no problem with what you want to do -- Perl will return as long a list from split as memory allows (see the documentation on split; if you specify a negative number as the third argument, it will return as many items as are there to be returned).

    If you don't know how many items there are going to be, you can just split your string into an array, e.g.:

    open IF, "$thefile" or die "Can't open $thefile: $!\n"; # note the 'or die' my @lines = <IF>; #load file into array, line by line close IF; foreach (@lines) { my @array = split ";", $_, -1; # no upper limit on # of items # process @array foreach my $item (@array) { #process $item } }

    Note, also, that your input routine, as written, will only grab the first line of the file.

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

Re: Split long string MANY times?
by lemming (Priest) on Feb 13, 2001 at 20:37 UTC
    Read up on split
    open (IF, "<$thefile"); $rawstring = (<IF>); close (IF);
    Do you want to read just the first line of your file?
    To split $rawstring and place into an array is as simple as @array = split(/;/, $rawstring);
    You don't need to escape the semi-colon and the < in the open is optional. Default case for open is read.