I'd hoped OP would correct or clarify the (possible disparity?) between the order of the inputs and the order of the outputs in the original post -- but given the time that's passed since the replies above encouraged her to do so, here's one approach (with enough bad ideas such as some tender regexen and making all variables global that you should NOT cargo cult it; turn it in for homework; or use in any production environment):
#!/usr/bin/perl -w
use strict;
#1159495
my $input = 'abcd| a a7-';
my ( $item, @display, $display, $out, );
my @deSpaced = split / /, $input;
for $item(@deSpaced) {
if ($item =~ /(a).*\|/ ) { # match first part of $input?
$out = '[' . $1 . ']';
push @display, $out;
}
if ( $item =~ /(^a$)/ ) { # match middle of $input?
$out = '[' . $1 . ']';
push @display, $out;
} elsif ( $item =~ /(a[0-9]{1}).+/ ) { # match last part of $i
+nput?
$out = '[' . $1 . ']';
push @display, $out;
}
}
foreach my $saved(@display) {
print $saved . ' ';
}
=head
EXECUTION:
C:\ PMonks\1159495.pl
[a] [a] [a7]
C:\
=cut
The code ignores ("leaves as an exercise for the Seeker") the re-ordering expressed in OP's desire to transform "abcd| a a7-" into "a a7 a"
The last element with an "a" in the original is "a7" (ignoring the hyphen) But your output makes the bracketed "a7" appear before a final (bracketed) "a."
Looks like a gimmé request! Sorry, we expect SOPW to seek wisdom, not to ask us to do so for them.
|