How to use regex on array? -


i find name tank2 in following array, do

#!/usr/bin/perl                                                                                            use strict; use warnings;  @out = ("aaa\n", "pool: tank2\n", "ccc\n"); foreach $line (@out) {$line =~ /pool: (.+)/; print $1;} 

and get

use of uninitialized value $1 in print tank2tank2 

my 2 problems are

  • for reason name printed twice , error.
  • how save result/name in variable when have been found first time?

pretty unreadable version :)

#!/usr/bin/perl                                                                                            use strict; use warnings;  @out = ("aaa\n", "pool: tank2\n", "ccc\n"); (my $var = (grep{/pool: (.+)/}@out)[0] )=~s/pool: //; print $var; 

and improved version:

#!/usr/bin/perl                                                                                            use strict; use warnings;  @out = ("aaa\n", "pool: tank2\n", "ccc\n"); $line (@out) {     print $1 if $line =~ /pool: (.+)/;  } 

and without $1:

my $var;     $line (@out){         print $var if ($var) = ($line =~ /pool: (.+)/);     } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -