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

I've got the following file that I'm splitting by |'s:

5|8|2#3#4#5|5

Which I figured would yield 4 elements, containing:

5
8
2#3#4#5
5

Now I want to split the 3rd element ($array[2]) by #s, but when I do so I get:

Use of uninitialized value at path/parse.pl line 50.

It works when there's only 1 value in the 3rd element but any more gives me the above error. Any thoughts?

Replies are listed 'Best First'.
Re: Double Splits?
by ryddler (Monk) on Dec 23, 2000 at 06:03 UTC
    I noticed two errors in your code. First was the quotes that Fastolfe pointed out above. The second was that you said you wanted to split on the third element of the array ($array[2]), but in your code you actually used "$split[1]".

    I used the following snippet of your code (with the proper array index) combined with the foreach loops I added to print out the values stored in your arrays for debugging. This is a technique I use regularly to find out if the arrays (or hashes) I use actually contain the values that I expect to be held.

    $html_this_file = "5|8|2#3#4#5|5"; my @split = split(/\|/, "$html_this_file"); my @prefs = split(/\#/, $split[2]); $count = 0; foreach (@split) { print "\$split[".$count++."] = $_\n"; } $count = 0; foreach (@prefs) { print "prefs ".$count++." = $_\n"; }

    HTH...
    ryddler

Re: Double Splits?
by ichimunki (Priest) on Dec 22, 2000 at 22:44 UTC
    This solves the problem as initially stated and doesn't rely on the x#y#z being in the same piece of the list, and it allows for multiple lines to be this way:
    $scalar = '1|2|3#4#5#6|789'; @list = split(/\|/,$scalar); @prefs = map {if (/#/) {split(/#/,$_);} } @list; print "\@list: " . join (' ', @list) . "\n"; print "\@prefs: ". join (' ', @prefs) . "\n";
    I make no guarantee as to speed, relevence, or any other thing. Also, if you want just the first list element that's got x#y#z in it, you will need to do away with map and substitute something like, <code>split( /#/, (include function to grab just first list element here) ).

Re: Double Splits?
by jeroenes (Priest) on Dec 23, 2000 at 03:30 UTC
    Since thursday, you may use Supersplit. To get your code in a two dimensional array, you can use this:
    use SuperSplit; $array = supersplit( '#', '\|', $text );
    Easy, isn't it?

    Cheers,

    Jeroen
    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

Re: Double Splits?
by Fastolfe (Vicar) on Dec 22, 2000 at 22:17 UTC
    It might help if we could see your code. There's no reason this shouldn't work.
      my @split = split(/\|/, "$html_this_file"); my $html_of_file = get $split[0]; my @prefs = split(/\#/, "$split[1]); my $j; for ($j = 0; $j <= $#prefs; $j++) { $html_of_file =~ s/$split[2]/<!--PARSE-->/s; $html_of_file =~ s/$split[3]/<!--PARSE-->/s; my @pieces = split(/<!--PARSE-->/, $html_of_file); $html_of_file = $pieces[1]; $html_of_file =~ s/<script.*\/script>//gis; $html_of_file =~ s/<style.*\/style>//gis; if ($prefs[$j] eq 'a') { $html_of_file =~ s/<a.*\/a>//gis; } elsif ($prefs[$j] eq 'all') { $html_of_file =~ s/<.*?>//gis; } else { $html_of_file =~ s/<$prefs[$j].*?>//gis; } }
        Is this a typo?
        my @prefs = split(/\#/, "$split[1]); ^
        In your loop, you are doing things that will only have an effect the first time through the loop. This may be the source of your warning (I have no idea what line matches up with the error message you got). Specifically, your substitutions, splits, etc., all act using things like @split and @pieces, which aren't really modified or differ between loops, since each loop is based off of @prefs. Sounds like you need to re-investigate your logic, or maybe just move those one-time operations outside your loop.