On Feb 23, 2014, at 5:10 AM, Wernher Eksteen <crypt...@gmail.com> wrote:

> Hi,
> 
> Thanks, but how do I assign the value found by the regex to a variable so 
> that the "1.2.4" from 6 file names in the array @fileList are print only 
> once, and if there are other versions found say 1.2.5 and 1.2.6 to print the 
> unique values from all.
> 
> 
> From that I want to get the value 1.2.4 and assign it to a variable, if there 
> are more than one value such as 1.2.5 and 1.2.6 as well, it should print them 
> too, but only the unique values.
> 
> My attempt shown below to print only the value 1.2.4 is as follow, but it 
> prints out "1.2.41.2.41.2.41.2.41.2.41.2.4" next to each other, if I pass a 
> newline to $i such as "$i\n" it then prints "111111" ?
> 
> foreach my $i (@fileList) {
>         print $i =~  /\b(\d+\.\d+\.\d+)\b/;
> }

The parentheses in the above regular expression cause the matched substrings to 
be assigned to $1. If you wish to print those values, print $1 or assign the 
value of $1 to another variable and print it:

  if( $i =~  /\b(\d+\.\d+\.\d+)\b/ ) {
    print “$1\n”;
  }

If you wish to find all of the unique values of what is captured, use the 
values as keys in a hash and print the keys after all the lines have been 
processed (untested):

my %unique;
foreach my $i (@fileList) {
  if( $i =~  /\b(\d+\.\d+\.\d+)\b/ ) {
    $unique{$1}++;
}
for my $number ( sort keys %unique ) {
  print “Version $number had $unique{$number} files\n”;
}


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to