in reply to Re^3: create array of empty files and then match filenames
in thread create array of empty files and then match filenames

The short answer is, with the code you've posted, you only populate @confs with one element (i.e. $confs) so there is nothing to sort!

When posting a question, you need to show us:

Input and output (including any messages) needs to be shown to us exactly as you see it, i.e. not prosaic descriptions of the same. All of this, and more, is explained in the "How do I post a question effectively?" guidelines: please read them.

In Re^4: create array of empty files and then match filenames, poj asked "What is in $confs?". You didn't really answer this question. Is it a series of Bnnn substrings, a series of filenames, or something else? A simple:

my $confs = '...';

in your code would have probably told us all we need to know.

From what you've posted here, and elsewhere in this thread, I'm wondering whether (as part of your learning) you haven't really understood how to populate arrays. Consider these two lines of code:

my @array1 = (1, 2, 3); my @array2 = '1, 2, 3';

@array1 contains three elements: the digits 1, 2 and 3. @array2 contains one element: the string '1, 2, 3'.

This also works the same for push which you seem to have used (probably incorrectly) in a number of places. Consider this code which results in @array1 and @array2 being populated exactly the same as in the last example:

my (@array1, @array2); my $elements_for_array = '1, 2, 3'; push @array1, 1, 2, 3; # @array1 has three elements push @array2, $elements_for_array; # @array2 has one element

The "push @array2, $elements_for_array;" example seems to be what you've done a few times and maybe has caused you problems. I'm really just guessing but perhaps that helps. See also "perlintro: Perl variable types" and "perldata: List value constructors".

In an attempt to get you back on track with your code, here's my guess as to the type of thing you need:

#!/usr/bin/env perl -l use strict; use warnings; my @filenames = qw{ MOLEC8-B040-OPT-FREQ2.gout MOLEC1-B001-OPT-FREQ2.gout MOLEC2-B010-OPT-FREQ2.gout MOLEC10-B002-OPT-FREQ2.gout }; my $re = qr{^[^-]+-([^-]+)}; my @sorted = sort { ($b =~ /$re/)[0] cmp ($a =~ /$re/)[0] } @filenames +; print 'Original filenames:'; print for @filenames; print 'Sorted filenames:'; print for @sorted;

Possibly the only tricky bits in that code are they parts like "($x =~ /$re/)[0]". $re contains one capture, i.e. ([^-]+), which gets the Bnnn part of each filename. In list context, the regex match evaluates to a list of the captures; as there's only one, ($x =~ /$re/) evaluates to something like ('Bnnn'). The zeroth index into that list evaluates to 'Bnnn' which provides a string for the cmp operation. [Also note that the regex I've used allows MOLECn to go above MOLEC9; the MOLEC10 example successfully tests this.]

Here's the output from running that script:

Original filenames: MOLEC8-B040-OPT-FREQ2.gout MOLEC1-B001-OPT-FREQ2.gout MOLEC2-B010-OPT-FREQ2.gout MOLEC10-B002-OPT-FREQ2.gout Sorted filenames: MOLEC8-B040-OPT-FREQ2.gout MOLEC2-B010-OPT-FREQ2.gout MOLEC10-B002-OPT-FREQ2.gout MOLEC1-B001-OPT-FREQ2.gout

— Ken