Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Create reference to sorted anonymous array

by Digioso (Sexton)
on Mar 16, 2016 at 14:28 UTC ( [id://1157939]=perlquestion: print w/replies, xml ) Need Help??

Digioso has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

my Google-Fu so far didn't produce any results for my question so I'm trying to obtain the answer here. :)
To keep it simple I creates a small test script. I'm creating a new array and give a reference of it to a function which simply prints the array elements.
Now I want to do the same for a reverse sorted array (basially reverse @array) without having to save the reverse output into a second array but so far I'm struggling to do that.
use strict; use warnings; sub blubb($) { my $array_ref = shift; for(my $i = 0; $i <= $#$array_ref; ++$i) { print "$i $array_ref->[$i]\n"; } } my @array = (1, 2, 3); blubb(\@array);
Now I could simply do:
my @array2 = reverse @array; blubb(\@array2);
My question is: Is there a way to do this without @array2 ?
Can I use blubb() directly with the reverse sorted @array ?

So far I haven't been able to come up with the syntax for this. :(

Thanks a lot in advance.

Replies are listed 'Best First'.
Re: Create reference to sorted anonymous array
by choroba (Cardinal) on Mar 16, 2016 at 14:32 UTC
      Thanks a lot. It's working with that. :)
      I'll also check out the link you posted.
Re: Create reference to sorted anonymous array
by Marshall (Canon) on Mar 17, 2016 at 00:48 UTC
    Another suggestion for you in addition to that from choroba .

    C style for() loops are actually quite rare in Perl. They are prone to the dreaded and most common programming error, the "off by one error". Here is another way to write blubb().

    use strict; use warnings; sub blubb { my $array_ref = shift; my $i =0; foreach (@$array_ref) #dereferences the ref to an array { print $i++, " $_\n"; } } my @array = (1, 2, 3); blubb([reverse @array]); __END__ Prints: 0 3 1 2 2 1
      Thanks for the tip. :)
      For me the C-Syntax has stuck somehow. The C-Loop provides a more readable syntax in my opinion and I'm trying to avoid using $_ as much as possible because someone else besides me might have to read my code. And when I learned programming my teachers told me to use variable names that state what they're used for. Although my school days are long gone I'm still sticking to that.
      OK, in my example @array and sub blubb are no such names obviously. :P
      Even this works btw:
      foreach (@$array_ref) { print; }
        If all you need is names, add them:
        for my $element (@array) { print $element, "\n"; }

        And, for completeness, iterating over indices:

        for my $index (0 .. $#array) { print "$index: $array[$index]\n"; }
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Further to choroba's reply: For even more complete completeness, if you're dealing with array references:

        for my $element (@$arrayref) { print $element, "\n"; } for my $i (0 .. $#$arrayref) { print "$i: $arrayref->[$i]\n"; }

        Update: Digioso: I quite agree with your preference for using explicitly named variables rather than default names (or none at all) in the interests of readability and maintainability. I disagree that the C-style for-loop serves these interests, quite apart from the lurking demon Offbyone.


        Give a man a fish:  <%-{-{-{-<

        As choroba and others have mentioned, I actually seldom use $_ and give an actual name to the var. This doesn't slow the code down a bit and makes it more readable.

        foreach my $line (@$array_ref) { print $line; }
        is just fine !!

        I write and lot of C code (and also ASM). I can tell you that the very, very most common error in programming is the "off by one" error. The  foreach() syntax avoids that possibility. There are a lot of programs out there that sometimes break in mysterious ways due to memory allocation errors and other issues related to the "off by one" problem. the foreach() syntax avoids this problem.

        You early instruction will serve you well. There is just a different way to do it in Perl than in C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1157939]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-24 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found