Sorting Arrays in Perl? - arrays

I am getting an error while trying to sort a simple array...
The ERROR reads: "use of uninitialized value in numeric comparison (<=>) at file.pl line #"
#!/usr/bin/perl
use strict
use wardings
use Data::Dumper
my #array
my $array
$array[1]= 5
$array[2]= 2
$array[3]= 3
$array[4]= 4
$array[5]= 1
sub numerically {$a <=> $b}
my #sortedarray = sort numerically #array;
print "#sortedarray\n";
I am just trying to sort the array to get:
1 2 3 4 5
I am new at perl so this might just be something stupid, but please help me... Thanks

Arrays are indexed starting at 0. The error comes from trying to sort the array when $array[0] is undefined.
Update: Also, in perl, one would write:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my #array = qw(1 2 3 4 5);
sub numerically {$a <=> $b}
my #sortedarray = sort numerically #array;
print "#sortedarray\n";
There is no point in declaring $array -- that would be a scalar. You are only working with the array #array, even though it is called with a $. Please read the perl documentation.

first of all, you need a semi-colon at the end of every statement. second, you're not using Data::Dumper, so why do you include it? You also don't need to declare the sub:
#!/usr/bin/env perl
use strict;
use warnings;
my #sorted = sort {$a <=> $b} qw (4 2 3 1 5);
print "#sorted\n";
And there we have it.

You're missing a shedload of semicolons.
It's warnings, not wardings.
Element 0 in your array is undefined.

Related

Replicate the array number of times in Perl

I have array which contains 5 elements (1,2,3,4,5). I want to replicate this a number of times based on the value set in the scalar $no_of_replication, e.g. 3.
So that my final array would contain (1,2,3,4,5,1,2,3,4,5,1,2,3,4,5).
Here is what I have tried. It gives me scalar content instead of elements.
use strict;
use warnings;
use Data::Dumper;
my #array = (1,2,3,4,5);
print Dumper(\#array);
my $no_of_replication = 3;
my #new_array = #array * $no_of_replication;
print Dumper(\#new_array);
My array(#new_array) should be like (1,2,3,4,5,1,2,3,4,5,1,2,3,4,5).
The operator for that is x and you need to be careful with the array syntax:
#new_array = ( #array ) x $no_of_replication;
Found the solution here:
Multiplying strings and lists in perl via Archive.org

How to access Array Item With Variable perl?

I'm new to perl.
I'm facing a problem to access items in array by variable like other languages eg. C C++ Python3 JavaScript
Expected Way To Do Same In Perl:
print "#array[$var]" ;
It should print value of array at $var.
But It Gives Error. Any other way To do same.
To access the value of an element of array #array, one uses $array[$i].
This is documented in perldata.
And yes, $array[$i] can be used in double-quoted string literals.
print("$array[$i]\n");
Note that #array[$i] also works, but with a warning. You should only use #array[...] when there's the possibility of getting multiple elements.
$ perl -e'
use strict;
use warnings;
my #array = "a".."z";
my $var = 2;
print "#array[$var]\n";
'
Scalar value #array[...] better written as $array[...] at -e line 7.
c
$ perl -e'
use strict;
use warnings;
my #array = "a".."z";
print "#array[2..4]\n";
'
c d e
Try this: $array[$var]. What you wrote before was an incorrect way to access an array slice. If you need a slice, try this: #array[$foo..$bar].

Perl: equivalent of "ubound" in VBA

Now that on day 2 of Perl I see that arrays start with element(0) too, how do I get the last index of an array like ubound in VBA rather than the size of it using scalar(#array)?
Is the use of $size = $#array a good way or is there something similar to scalar(#array)?
Perl's arrays always start empty.
my #array; # equivalent
my #array = (); #
To get the highest index, use $#array.
my #array = qw(a b c);
print $#array; # 2
If the array is empty, $#array will be -1.

How do i count the number of specific letters in an array, Perl?

I have an array which only has letters in it, no digits. I want to count the number of times a specific letter shows. I don't want to use a hash, i need to preserve the order of the list.
use strict;
use warnings;
my %counts;
$counts{$_}++ for #array;
print "$counts\n";
The code that you have seems to work find for counting the occurrences. The only problem you have is in displaying the counts. You're using a new scalar variable called $counts which is undeclared and empty.
What you want is this:
use strict;
use warnings;
my %counts;
$counts{$_}++ for #array;
print "$_: $counts{$_}\n" for keys %counts;
Use grep for to do it
my #ar = qw(abc cde fgh 123 abc);
my $count = grep{ /ab/} #ar;
print $count;
Or else use foreach
my #ar = qw(abc cde fgh 123 abc);
my $m;
$m+= /ab/,foreach (#ar);
print $m;
While the match was encountered $m will increment.
You might consider Text::CountString module on CPAN.

Perl array element comparing

I am new in Perl programming. I am trying to compare the two arrays each element. So here is my code:
#!/usr/bin/perl
use strict;
use warnings;
use v5.10.1;
my #x = ("tom","john","michell");
my #y = ("tom","john","michell","robert","ricky");
if (#x ~~ #y)
{
say "elements matched";
}
else
{
say "no elements matched";
}
When I run this I get the output
no elements matched
So I want to compare both array elements in deep and the element do not matches, those elements I want to store it in a new array. As I can now compare the only matched elements but I can't store it in a new array.
How can I store those unmatched elements in a new array?
Please someone can help me and advice.
I'd avoid smart matching in Perl - e.g. see here
If you're trying to compare the contents of $y[0] with $x[0] then this is one way to go, which puts all non-matches in an new array #keep:
use strict;
use warnings;
use feature qw/say/;
my #x = qw(tom john michell);
my #y = qw(tom john michell robert ricky);
my #keep;
for (my $i = 0; $i <$#y; $i++) {
unless ($y[$i] eq $x[$i]){
push #keep, $y[$i];
}
}
say for #keep;
Or, if you simply want to see if one name exists in the other array (and aren't interested in directly comparing elements), use two hashes:
my (%x, %y);
$x{$_}++ for #x;
$y{$_}++ for #y;
foreach (keys %y){
say if not exists $x{$_};
}
It would be well worth your while spending some time reading the Perl FAQ.
Perl FAQ 4 concerns Data Manipulation and includes the following question and answer:
How do I compute the difference of two arrays? How do I compute
the intersection of two arrays?
Use a hash. Here's code to do both and more. It assumes that each
element is unique in a given array:
my (#union, #intersection, #difference);
my %count = ();
foreach my $element (#array1, #array2) { $count{$element}++ }
foreach my $element (keys %count) {
push #union, $element;
push #{ $count{$element} > 1 ? \#intersection : \#difference }, $element;
}
Note that this is the symmetric difference, that is, all elements
in either A or in B but not in both. Think of it as an xor
operation.

Resources