diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2019-12-23 17:36:34 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2019-12-23 17:36:34 +0100 |
| commit | 1899cacac7ca2813593cce58dec0fc8fb0598c97 (patch) | |
| tree | f69ace7cba6a1e9108c48d7ff6e222ef28713e2e | |
| parent | 03f344f3bf8f23db23cb389161c8e941fda4612f (diff) | |
| download | perlweeklychallenge-club-1899cacac7ca2813593cce58dec0fc8fb0598c97.tar.gz perlweeklychallenge-club-1899cacac7ca2813593cce58dec0fc8fb0598c97.tar.bz2 perlweeklychallenge-club-1899cacac7ca2813593cce58dec0fc8fb0598c97.zip | |
Perl solutions LK 040
| -rw-r--r-- | challenge-040/lubos-kolouch/perl5/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-040/lubos-kolouch/perl5/ch-2.pl | 43 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-040/lubos-kolouch/perl5/ch-1.pl b/challenge-040/lubos-kolouch/perl5/ch-1.pl new file mode 100644 index 0000000000..e880c05e44 --- /dev/null +++ b/challenge-040/lubos-kolouch/perl5/ch-1.pl @@ -0,0 +1,35 @@ +#!env perl +use strict; +use warnings; +use feature qw /say/; +use Data::Dumper; + + +my $array_ref = shift // die 'No arrays passed'; + +my @arr = $array_ref; + +die "Usage ch-1.pl \"[1 2 3 4][5 6 7 8]\"" unless ($arr[0] =~ / ^(\[(. *)+\])+$/msx); + +my $arr_size; + +my @all_array; + +my $i=0; +for (split /\]/, $arr[0]) { + $_ =~ s/\[//g; + + push @{$all_array[$i]}, split / /; + + $arr_size = scalar @{$all_array[$i]} unless $arr_size; + + die "arrays are not of the same size" unless scalar @{$all_array[$i]} == $arr_size; + $i++; +} + +for my $key (0..$arr_size-1) { + for my $arr (0..scalar @all_array -1) { + print $all_array[$arr][$key]." "; + } + say ''; +} diff --git a/challenge-040/lubos-kolouch/perl5/ch-2.pl b/challenge-040/lubos-kolouch/perl5/ch-2.pl new file mode 100644 index 0000000000..40f6d591f9 --- /dev/null +++ b/challenge-040/lubos-kolouch/perl5/ch-2.pl @@ -0,0 +1,43 @@ +#!env perl +use strict; +use warnings; +use feature qw /say/; +use Data::Dumper; + + +my $array_ref = shift // die 'No arrays passed'; + +my @arr = $array_ref; + +die "Usage ch-2.pl \"[10 4 1 8 12 3][0 2 5]\"" unless ($arr[0] =~ / ^(\[(\d+\h*)+\])+$/msx); + +my $arr_size; + +my @all_array; + +my $i=0; +for (split /\]/, $arr[0]) { + $_ =~ s/\[//g; + + push @{$all_array[$i]}, split / /; + + $arr_size = scalar @{$all_array[$i]} unless $arr_size; + + $i++; +} + +my @sort_array; + +for (@{$all_array[1]}) { + push @sort_array, $all_array[0][$_]; +} + +@sort_array = sort {$a <=> $b} @sort_array; + +$i=0; +for (@{$all_array[1]}) { + $all_array[0][$all_array[1][$i]] = $sort_array[$i]; + $i++; +} + +warn Dumper \$all_array[0]; |
