#!/usr/bin/perl -w use strict; my @compounds=("C3H8O2", "C6H10O5", "C36H62O31", "C5H16N2", "C10H11N5O6P", "C9H11N3O7P", "C3H9N2O2"); print join("\n", sort{ my ($num_A) = ($a =~ /^C(\d+)/); my ($num_B) = ($b =~ /^C(\d+)/); $num_A <=> $num_B } grep { /P$/ } #could be just /P/? @compounds),"\n"; __END__ C9H11N3O7P C10H11N5O6P