Print 10 elements out of array [duplicate] - perl

This question already has answers here:
What's the best way to get the last N elements of a Perl array?
(6 answers)
Closed 7 years ago.
If i have an array that I've used to create 50 random numbers, I then sort them numerically. Now lets say I wanted to print out the 10 biggest number (elements 40 to 50) I could say:
print($array[40]) print($array[41]) print($array[42]) etc etc.
But is there a neater way to it? Hope I'm making myself clear.
Cheers

You could loop over the indexes.
say $array[$_] for 40..49;
Offsets from the end make more sense here.
say $array[$_] for -10..-1;
You could also use an array slice.
say for #array[-10..-1];
To print them on one line, you can use join.
say join ', ', #array[-10..-1];

Try this :
print join ',',#array[-10..-1] ,"\n";

Related

Why does this not give the 2nd last element of the array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
print $some_array[$#some_array - 1];
My intuitive understanding is the above code should print the 2nd last element in #some_array.
However in reality it does not. It prints something I cannot made sense of.
It does print the second to last element in the array:
my #nums = qw(a b c d e);
print "$nums[$#nums-1]\n"; # prints 'd'
Here, $#nums gives the last index of an array (in this case 4 - arrays are zero-based), and hence $#nums - 1 gives array element 3.
A more common way of writing this would be:
$array[-2]
The behavior of $some_array[$#some_array - 1] depends on how many elements #some_array has.
If #some_array has 2 or more elements (scalar #array >= 2, or equivalently $#array >= 1), then your statement:
print $some_array[$#some_array - 1];
should work correctly. Say the array has 10 elements, then $#some_array == 9, and you'll print $some_array[8], the second-to-last element.
If #some_array has just one element, then $#some_array == 0, and you'll print the "-1th" element. Negative array indices are treated specially: -1 gives you the last element of the array, -2 the second-to-last, and so forth. And if your goal is to print the second-to-last element of the array, you should be using this feature.
If #some_array is empty (scalar #some_array == 0, $#some_array == -1), then your statement will try to print $some_array[-2], which doesn't exist, resulting in an error message.

Need help understanding perl code [duplicate]

This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.

creating a hash with regex matches in perl

Lets say i have a file like below:
And i want to store all the decimal numbers in a hash.
hello world 10 20
world 10 10 10 10 hello 20
hello 30 20 10 world 10
i was looking at this
and this worked fine:
> perl -lne 'push #a,/\d+/g;END{print "#a"}' temp
10 20 10 10 10 10 20 30 20 10 10
Then what i need was to count number of occurrences of each regex.
for this i think it would be better to store all the matches in a hash and assign an incrementing value for each and every key.
so i tried :
perl -lne '$a{$1}++ for ($_=~/(\d+)/g);END{foreach(keys %a){print "$_.$a{$_}"}}' temp
which gives me an output of:
> perl -lne '$a{$1}++ for ($_=~/(\d+)/g);END{foreach(keys %a){print "$_.$a{$_}"}}' temp
10.4
20.7
Can anybody correct me whereever i was wrong?
the output i expect is:
10.7
20.3
30.1
although i can do this in awk,i would like to do it only in perl
Also order of the output is not a concern for me.
$a{$1}++ for ($_=~/(\d+)/g);
This should be
$a{$_}++ for ($_=~/(\d+)/g);
and can be simplified to
$a{$_}++ for /\d+/g;
The reason for this is that /\d+/g creates a list of matches, which is then iterated over by for. The current element is in $_. I imagine $1 would contain whatever was left in there by the last match, but it's definitely not what you want to use in this case.
Another option would be this:
$a{$1}++ while ($_=~/(\d+)/g);
This does what I think you expected your code to do: loop over each successful match as the matches happen. Thus the $1 will be what you think it is.
Just to be clear about the difference:
The single argument for loop in Perl means "do something for each element of a list":
for (#array)
{
#do something to each array element
}
So in your code, a list of matches was built first, and only after the whole list of matches was found did you have the opportunity to do something with the results. $1 got reset on each match as the list was being built, but by the time your code was run, it was set to the last match on that line. That is why your results didn't make sense.
On the other hand, a while loop means "check if this condition is true each time, and keep going until the condition is false". Therefore, the code in a while loop will be executed on each match of a regex, and $1 has the value for that match.
Another time this difference is important in Perl is file processing. for (<FILE>) { ... } reads the entire file into memory first, which is wasteful. It is recommended to use while (<FILE>) instead, because then you go through the file line by line and keep only the information you want.

How to retrieve the values of required keys in Perl [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a hash with keys and values. How can I retrieve the values of the desired keys?
%a = qw(genea brain geneb heart genec kidney gened eye);
Now I want to retrieve the value for the keys genec and gened. How can I do this?
To get a list of the values for many keys at once, use a hash slice:
#lots_of_values = #hash{ #lots_of_keys };
Because a list is the result, you use the # sigil even though it is a hash; the values will be the order of the keys specified, including undef values where the specified keys don't exist in the hash.
It sounds like all you're asking is how to access elements of a hash. As Quentin indicates, this is trivially google-able.
The perldata doc covers basic questions, and perlfaq4 covers many other hash questions.
That said, to answer your question:
print $a{'genec'};
print $a{'gened'};
I also would not declare your hash in that way, as it's unclear what is a key and what is a value. Instead, consider:
my %a = ('genea' => 'brain', 'geneb' => 'heart'); # etc.
$GENEC = $a{genec};
$GENED = $a{gened};
Please get yourself a copy of Learning Perl. You'll be glad you did.

Find combinations of numbers that sum to some desired number

I need an algorithm that identifies all possible combinations of a set of numbers that sum to some other number.
For example, given the set {2,3,4,7}, I need to know all possible subsets that sum to x. If x == 12, the answer is {2,3,7}; if x ==7 the answer is {{3,4},{7}} (ie, two possible answers); and if x==8 there is no answer. Note that, as these example imply, numbers in the set cannot be reused.
This question was asked on this site a couple years ago but the answer is in C# and I need to do it in Perl and don't know enough to translate the answer.
I know that this problem is hard (see other post for discussion), but I just need a brute-force solution because I am dealing with fairly small sets.
sub Solve
{
my ($goal, $elements) = #_;
# For extra speed, you can remove this next line
# if #$elements is guaranteed to be already sorted:
$elements = [ sort { $a <=> $b } #$elements ];
my (#results, $RecursiveSolve, $nextValue);
$RecursiveSolve = sub {
my ($currentGoal, $included, $index) = #_;
for ( ; $index < #$elements; ++$index) {
$nextValue = $elements->[$index];
# Since elements are sorted, there's no point in trying a
# non-final element unless it's less than goal/2:
if ($currentGoal > 2 * $nextValue) {
$RecursiveSolve->($currentGoal - $nextValue,
[ #$included, $nextValue ],
$index + 1);
} else {
push #results, [ #$included, $nextValue ]
if $currentGoal == $nextValue;
return if $nextValue >= $currentGoal;
}
} # end for
}; # end $RecursiveSolve
$RecursiveSolve->($goal, [], 0);
undef $RecursiveSolve; # Avoid memory leak from circular reference
return #results;
} # end Solve
my #results = Solve(7, [2,3,4,7]);
print "#$_\n" for #results;
This started as a fairly direct translation of the C# version from the question you linked, but I simplified it a bit (and now a bit more, and also removed some unnecessary variable allocations, added some optimizations based on the list of elements being sorted, and rearranged the conditions to be slightly more efficient).
I've also now added another significant optimization. When considering whether to try using an element that doesn't complete the sum, there's no point if the element is greater than or equal to half the current goal. (The next number we add will be even bigger.) Depending on the set you're trying, this can short-circuit quite a bit more. (You could also try adding the next element instead of multiplying by 2, but then you have to worry about running off the end of the list.)
The rough algorithm is as follows:
have a "solve" function that takes in a list of numbers already included and a list of those not yet included.
This function will loop through all the numbers not yet included.
If adding that number in hits the goal then record that set of numbers and move on,
if it is less than the target recursively call the function with the included/exluded lists modified with the number you are looking at.
else just go to the next step in the loop (since if you are over there is no point trying to add more numbers unless you allow negative ones)
You call this function initially with your included list empty and your yet to be included list with your full list of numbers.
There are optimisations you can do with this such as passing the sum around rather than recalculating each time. Also if you sort your list initially you can do optimisations based on the fact that if adding number k in the list makes you go over target then adding k+1 will also send you over target.
Hopefully that will give you a good enough start. My perl is unfortuantely quite rusty.
Pretty much though this is a brute force algorithm with a few shortcuts in it so its never going to be that efficient.
You can make use of the Data::PowerSet module which generates all subsets of a list of elements:
Use Algorithm::Combinatorics. That way, you can decide ahead of time what size subsets you want to consider and keep memory use to a minimum. Apply some heuristics to return early.
#!/usr/bin/perl
use strict; use warnings;
use List::Util qw( sum );
use Algorithm::Combinatorics qw( combinations );
my #x = (1 .. 10);
my $target_sum = 12;
{
use integer;
for my $n ( 1 .. #x ) {
my $iter = combinations(\#x, $n);
while ( my $set = $iter->next ) {
print "#$set\n" if $target_sum == sum #$set;
}
}
}
The numbers do blow up fairly rapidly: It would take thousands of days to go through all subsets of a 40 element set. So, you should decide on the interesting sizes of subsets.
Is this a 'do my homework for me' question?
To do this deterministically would need an algorithm of order N! (i.e. (N-0) * (N-1) * (N-2)...) which is going to be very slow with large sets of inputs. But the algorithm is very simple: work out each possible sequence of the inputs in the set and try adding up the inputs in the sequence. If at any point the sum matches, you've got one of the answers, save the result and move on to the next sequence. If at any point the sum is greater than the target, abandon the current sequence and move on to the next.
You could optimize this a little by deleting any of the inputs greater than the target. Another approach for optimization would be to to take the first input I in the sequence and create a new sequence S1, deduct I from the target T to get a new target T1, then check if T exists in S1, if it does then you've got a match, otherwise repeat the process with S1 and T1. The order is still N! though.
If you needed to do this with a very large set of numbers then I'd suggest reading up on genetic algorithms.
C.
Someone posted a similar question a while ago and another person showed a neat shell trick to answer it. Here is a shell technique, but I don't think it is as neat a solution as the one I saw before (so I'm not taking credit for this approach). It's cute because it takes advantage of shell expansion:
for i in 0{,+2}{,+3}{,+4}{,+7}; do
y=$(( $i )); # evaluate expression
if [ $y -eq 7 ]; then
echo $i = $y;
fi;
done
Outputs:
0+7 = 7
0+3+4 = 7