in reply to "Experimental push on scalar is now forbidden"
Given your Perl is rusty, I'd like to suggest some alternatives to the approach you are currently taking.
From your sample code, I don't like:
because the repeated tests for "and" violate DRY, a fundamental principle of software development.if ( $elt ne "and" ) { push ( $elt, @{ $bucket[$counter] } ); } if ( $elt eq "and" ) { $counter++; }
Two alternative ways to remedy are given below:
use strict; use warnings; use Data::Dumper; my @things = ( 1, 2, 3, "and", 4, 5, "and", 7, 8, 9 ); my @bucket; my $counter = 0; foreach my $elt ( @things ) { if ($elt eq "and") { $counter++; } else { push @{ $bucket[$counter] }, $elt; } } print "counter = $counter\n", Dumper(\@bucket);
which prints:
counter = 2 $VAR1 = [ [ 1, 2, 3 ], [ 4, 5 ], [ 7, 8, 9 ] ];
If you are parsing strings, the Perl split function seems well-suited. For example:
use strict; use warnings; use Data::Dumper; my $things = 'this and that and another brigand and a bandicoot'; my @bucket = split /\band\b/, $things; print Dumper(\@bucket);
which prints:
$VAR1 = [ 'this ', ' that ', ' another brigand ', ' a bandicoot' ];
If you describe your problem in more detail, we may be able to suggest better ways to do it than your current approach.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "Experimental push on scalar is now forbidden"
by building_arch (Novice) on Dec 20, 2021 at 21:58 UTC |