I have a big array and I want to sort all the elements of the array in alphabetical order.
In a previous subroutine, the element of the array are being pushed to tc_reg array.
I have an array named #tc_lane. When I print element of the array it would look something like this
tx0_abc
rx0_fgw
ref_ghv
..
Now I want to sort this array like this,
ref_ghv
rx0_fgw
tx_abc
..
If you want
rx0_fgw
rx10_fgw
rx2_fgw
use
my #sorted = sort #unsorted;
If you want
rx0_fgw
rx2_fgw
rx10_fgw
use
use Sort::Key::Natural qw( natsort );
my #sorted = natsort #unsorted;
You simply need to do:
#tc_lane = sort #tc_lane;
Related
The thing that I want to do is to compare the two arrays, find out if any duplication has been made in the second array versus the first array. Then if there has been any duplication, remove it from the second array so that if they select the same value in both lists that it will only be in the first.
first array (1,2,3,6,7.9)
second array (4,5,6,9,10,11)
results would be
first array (1,2,3,6,7,9)
second array (4,5,10,11)
$first = array (1,2,3,6,7,9);
$second = array (4,5,10,11);
foreach ($second as $k=>$v){
if(in_array($v, $first)){
unset($second[$k]);
}
}
$second = array_values($second);
print_r($second); //Output
I have a 3 dimensional array. I want to set three elements of it like this:
$array[$x][$y][0 .. 2] = (0, 1, 2);
but perl tells me:
Useless use of a constant (1) in void context
In array context:
#array[$x][$y][0 .. 2] = (0, 1, 2);
but perl tells me:
syntax error near "]["
presumably meaning that it expects me to give it two indices and then assign to the third dimension as a separate array? However, on this page, under Example: Assignment Using Array Slices, it suggests that it is possible to assign to a slice using the range operator where it says:
#array1[1..3] = #array2[23..25];
How can I assign to a slice of the array like this, or do I have to assign each index individually?
You need to dereference the inner array:
#{ $arr[$x][$y] }[ 0 .. 2 ] = (0, 1, 2);
$array[$x][$y][0..2] isn't a slice; it's just an element lookup.
When you attempted to change it into a slice, you sliced the wrong array. You sliced #arr instead of #{ $arr[$x][$y] }.
The key here is to realize that there's no such thing as 3d arrays in Perl. What you have is an array of references to arrays of references to array, which is colloquially called array of array of array, and often abbreviated to AoAoA.
Array slices have the following syntax:
#NAME[LIST]
#BLOCK[LIST]
#$REF[LIST]
EXPR->#[LIST][1]
You could use any of the following:
The first syntax can't be used since the array to slice doesn't have a name.
#{ $array[$x][$y] }[0..2] = 0..2;
my $ref = $array[$x][$y]; #$ref[0..2] = 0..2;
$array[$x][$y]->#[0..2] = 0..2;[1]
See Dereferencing Syntax.
Requires Perl 5.24+. Available in Perl 5.20+ by adding both use feature qw( postderef ); and no warnings qw( experimental::postderef );.
I need to take two arrays and return a single array with the combination of the items in them, listing the first items first. Like so:
combinations(["on","in"],["to","rope"])
# => ["onto","onrope","into","inrope"]
I've written a method that does this, but after that, I can't figure out where to go.
Use Array#product:
["on","in"].product(["to","rope"]).map(&:join)
# => ["onto", "onrope", "into", "inrope"]
def combinations(ary1, ary2)
ary1.map {|i| ary2.map {|i2| "#{i}#{i2}" }}.flatten
end
I'm trying to sort an array which is a value in a hash. The following line of code:
sort #{ $hash{$item}{'lengths'} };
produces the following error:
Useless use of sort in void context at ...
In Perl, sort does not modify the array; it returns a sorted list. You have to assign that list somewhere (either back into the original array, or somewhere else).
#{ $hash{$item}{'lengths'} } = sort #{ $hash{$item}{'lengths'} };
Or, (especially if the array is deep in a nested hash):
my $arrayref = $hash{$item}{'lengths'};
#$arrayref = sort #$arrayref;
Your original code was sorting the array, and then throwing away the sorted list, which is why it produces that warning.
Note: As salva pointed out, by default sort does a string comparison. You probably wanted a numeric sort, which you get by using sort { $a <=> $b } instead of just sort:
my $arrayref = $hash{$item}{'lengths'};
#$arrayref = sort { $a <=> $b } #$arrayref;
But that has nothing to do with the warning message you asked about.
Perl's sort does not re-order the list "in-place". It actually makes a copy of the list and then sorts and returns . so do it as cjm suggested,
you can read the similar problem on this link (see the example part)
Note also, that if you are sorting lenghts, you will have to use a numeric comparison:
my $lengths = $hash{$item}{'lengths'};
#$lengths = sort { $a <=> $b } #$lengths;
I tried something like this
my %lrn_hash;
$lrn_hash{1} = 1;
#{$lrn_hash{1}{VALS}} = (6,7,1,5,7,9);
#narr = sort #{$lrn_hash{1}{VALS}};
print "#narr\n";
and it worked fine and the output was
1 5 6 7 7 9
Wich perl version u are using ?
The way a hash is structed can always vary, it can be a hash of a hash of an array or whatever.
And for every different struct of a hash there needs to be a different implementation of turning it into a two dimensional array.
Is there a general way of converting a hash into an array?
Such that i could say, for instance, first key becomes column 0, second key column 1 etc.
Example from comments:
$distangle{some_distance}{some_angle}=();
now I want to convert that hash of hashes into an ordinary two dimensional array #distangle=(some_distance,some_angle).
That's a method, then tomorrow I have some different form of a hash I also need to convert to a two dimensional array.
Firstly, hashes are unordered, so when you say the "first key", there's no such thing.
Secondly, if you do have a hash of hash of arrays (as in your example), then it seems to me that the strict requirement of reducing it to a 2-dimensional array will result in data-loss (assuming you mean that none of the elements in that array can be hashrefs or arrayrefs).
What is "first" key in a hash? The keys are not ordered. Do you want to order them alphabetically?
#arr = map { $hash{$key} } sort keys %hash;
EDIT:
OP wants a 2D array, so here it is:
#arr = ()
for $first (keys %hash) {
for $second (keys %{ $hash{$first} }) {
for $third (keys %{ $hash{$first}{$second} }) {
my $value = $hash{$first}{$second}{$third};
push #arr, ($first, $second, $third, $value);
Something like this?
EDIT 2: This solution looks nice too.