Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Returning an array the way it was written

by Arengin (Novice)
on Mar 15, 2017 at 14:03 UTC ( [id://1184699]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I have the following array:

XX, AA, BC, DD
And I want the output of the print just like that.
But instead it sorts them alphabetically with

print(select, [%names]);

The sort options I found were not good either.

Any help out there?

Thank you so much.

Arengin

Replies are listed 'Best First'.
Re: Returning an array the way it was written
by hippo (Bishop) on Mar 15, 2017 at 14:48 UTC

    As stated in my reply to your previous, similar question the percent sign (here in %names) indicates a hash, not an array and does not retain a natural order. Use an array, not a hash.

    Here's an SSCCE to demonstrate:

    #!/usr/bin/env perl use strict; use warnings; my @names = qw/XX AA BC DD/; print "@names\n";

    Further note that you are not returning anything here, merely outputting it.

Re: Returning an array the way it was written
by marto (Cardinal) on Mar 15, 2017 at 14:48 UTC
Re: Returning an array the way it was written
by ww (Archbishop) on Mar 15, 2017 at 14:56 UTC

    If your "array" is %names, that isn't an array in Perl's terminology; it's a hash; what PHP calls an "associative array."

    And hashes do NOT maintain their input order (without the likes of Tie::Hash. If it's perl (nod to Vicar 1nickt, above) you're using, you might consider that. But first, read some of the help about arrays and hashes you'll find here... in Tutorials or Super Search and perhaps even read perldoc perltie (from your CLI).

    And does the select part of line 1 means you're trying to print something pulled from a db? If so, Super Search and the Tutorials should also offer assistance.

Re: Returning an array the way it was written
by 1nickt (Canon) on Mar 15, 2017 at 14:12 UTC

    That doesn't look like Perl code.

    In Perl the whole point of an array is that it maintains order.

    my @x = (3, 1, 2); print "@x";
    Output:
    3 1 2


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found