Skip to main content
Rollback to Revision 4
Source Link
t3chb0t
  • 44.7k
  • 9
  • 85
  • 191

Extract the strings from nested arrays in Perl.

It prints: a, b, c, d, E

use strict;
use warnings;
use feature qw(say signatures current_sub); 
no warnings qw(experimental::signatures);

my $nested  = [1, 'a', [2, 3, 'b'], [4, [5, 6, 7, ['c']], [7, [8, 9, [10, 'd']]], 11, 'E']];
my $pattern = qr/^[a-zA-Z]/;

print join ', ', @{ extract_strings($nested, $pattern) };

sub extract_strings($input, $pattern) {
    my @output = ();
    
    my $loop = sub ($val) {
        if (ref $val eq 'ARRAY') {
            __SUB__->($_) for @{ $val };
        } else {
            push(@output, $val) if $val =~ $pattern;/^[a-zA-Z]/;
        }
    };

    $loop->($input);
    
    return \@output;
}

Any idea for improvement without not core dependencies?

Extract the strings from nested arrays in Perl.

It prints: a, b, c, d, E

use strict;
use warnings;
use feature qw(say signatures current_sub); 
no warnings qw(experimental::signatures);

my $nested  = [1, 'a', [2, 3, 'b'], [4, [5, 6, 7, ['c']], [7, [8, 9, [10, 'd']]], 11, 'E']];
my $pattern = qr/^[a-zA-Z]/;

print join ', ', @{ extract_strings($nested, $pattern) };

sub extract_strings($input, $pattern) {
    my @output = ();

    my $loop = sub ($val) {
        if (ref $val eq 'ARRAY') {
            __SUB__->($_) for @{ $val };
        } else {
            push(@output, $val) if $val =~ $pattern;
        }
    };

    $loop->($input);

    return \@output;
}

Any idea for improvement without not core dependencies?

Extract the strings from nested arrays in Perl.

It prints: a, b, c, d, E

use strict;
use warnings;
use feature qw(say signatures current_sub); 
no warnings qw(experimental::signatures);

my $nested = [1, 'a', [2, 3, 'b'], [4, [5, 6, 7, ['c']], [7, [8, 9, [10, 'd']]], 11, 'E']];

print join ', ', @{ extract_strings($nested) };

sub extract_strings($input) {
    my @output = ();
    
    my $loop = sub ($val) {
        if (ref $val eq 'ARRAY') {
            __SUB__->($_) for @{ $val };
        } else {
            push(@output, $val) if $val =~ /^[a-zA-Z]/;
        }
    };

    $loop->($input);
    
    return \@output;
}

Any idea for improvement without not core dependencies?

Extracted match pattern as a parameter.
Source Link

Extract the strings from nested arrays in Perl.

It prints: a, b, c, d, E

use strict;
use warnings;
use feature qw(say signatures current_sub); 
no warnings qw(experimental::signatures);

my $nested  = [1, 'a', [2, 3, 'b'], [4, [5, 6, 7, ['c']], [7, [8, 9, [10, 'd']]], 11, 'E']];
my $pattern = qr/^[a-zA-Z]/;

print join ', ', @{ extract_strings($nested, $pattern) };

sub extract_strings($input, $pattern) {
    my @output = ();
    
    my $loop = sub ($val) {
        if (ref $val eq 'ARRAY') {
            __SUB__->($_) for @{ $val };
        } else {
            push(@output, $val) if $val =~ /^[a-zA-Z]/;$pattern;
        }
    };

    $loop->($input);
    
    return \@output;
}

Any idea for improvement without not core dependencies?

Extract the strings from nested arrays in Perl.

It prints: a, b, c, d, E

use strict;
use warnings;
use feature qw(say signatures current_sub); 
no warnings qw(experimental::signatures);

my $nested = [1, 'a', [2, 3, 'b'], [4, [5, 6, 7, ['c']], [7, [8, 9, [10, 'd']]], 11, 'E']];

print join ', ', @{ extract_strings($nested) };

sub extract_strings($input) {
    my @output = ();
    
    my $loop = sub ($val) {
        if (ref $val eq 'ARRAY') {
            __SUB__->($_) for @{ $val };
        } else {
            push(@output, $val) if $val =~ /^[a-zA-Z]/;
        }
    };

    $loop->($input);
    
    return \@output;
}

Any idea for improvement without not core dependencies?

Extract the strings from nested arrays in Perl.

It prints: a, b, c, d, E

use strict;
use warnings;
use feature qw(say signatures current_sub); 
no warnings qw(experimental::signatures);

my $nested  = [1, 'a', [2, 3, 'b'], [4, [5, 6, 7, ['c']], [7, [8, 9, [10, 'd']]], 11, 'E']];
my $pattern = qr/^[a-zA-Z]/;

print join ', ', @{ extract_strings($nested, $pattern) };

sub extract_strings($input, $pattern) {
    my @output = ();

    my $loop = sub ($val) {
        if (ref $val eq 'ARRAY') {
            __SUB__->($_) for @{ $val };
        } else {
            push(@output, $val) if $val =~ $pattern;
        }
    };

    $loop->($input);

    return \@output;
}

Any idea for improvement without not core dependencies?

There is nothing about linked-lists in this question or code, so I removed it and replace it with more appropriate tags.
Link
Edited the regexp match in the `else` section of the `loop` sub.
Source Link
Loading
Renamed function the function to `extract_strings`
Source Link
Loading
Tweeted twitter.com/StackCodeReview/status/1171121278893330432
Source Link
Loading