LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Use of uninitialized value in pattern match - perl (https://www.linuxquestions.org/questions/programming-9/use-of-uninitialized-value-in-pattern-match-perl-888455/)

casperdaghost 06-26-2011 01:12 PM

Use of uninitialized value in pattern match - perl
 
I want to strip the process name from the hosts - i did it with the code below.

I have two questions - is there a more compact way to strip off the process names? usalso i want to get rid of the errors after extracting the hostname. It is complaing about $arry[1]. using my $arry[1] is not allowed. Assigning the slice to a value, as is 'my $sliced_arry = $arry[1]; print $sliced_arry , does not work either.
Code:



#!/usr/bin/perl -w
use strict;
  open NEWCOMM, "newcomm_stat -n local mis100 |";
  while (<NEWCOMM>) {
            my @arry  = split (/\s+/ , $_);
            print "$arry[1]\n";
            }
(zeusUS@mit201
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 10
(odin@ibmis100
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 12
(thorUS@mit201
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 14
(balder@mis102
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 16

here i get what i want - just the host name, but still get those nasty errors. assigning a value to $1 does not work, and localizing $1 with 'my' is not allowed.

Code:

#!/usr/bin/perl -w
use strict;
  open NEWCOMM, "newcomm_stat -n localtick ibmis100 |";
  while (<NEWCOMM>) {
            my @arry  = split (/\s+/ , $_);
            if ($arry[1] =~ /\(\w+@(\w+)/){
                  my $newcomm_hosts =  $1;
                  print "$newcomm_hosts\n";
                  sleep 1;
            }
  }



mit102
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 10.
ibmis100
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 12.
ibmis100
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 14.
mit201
Use of uninitialized value in pattern match (m//) at newcomm_stats.pl3 line 7, <NEWCOMM> line 26.


Sergei Steshenko 06-26-2011 01:45 PM

For starters, you didn't give us info what line #7 is.

I suggest to

cat -n your_file.pl

and copy-paste screen output of the above here.
...
Read carefully 'perdoc -f split' - near the end. Check values of '@arry'. Consider using "split(' ', $whatever)" instead of "split(/\s+/, $whatever)" - you practically never need the latter (I don't remember such a case in my life).

casperdaghost 06-26-2011 02:19 PM

thank you i am reading it now

Sergei Steshenko 06-26-2011 02:42 PM

Quote:

Originally Posted by casperdaghost (Post 4396321)
thank you i am reading it now

I meant

Code:

split(' ', $whatever);
, i.e. space as first argument.

casperdaghost 06-27-2011 07:31 PM

I read the perl doc's for split, and ended up traversing the array with a for each loop - use strict seems to like that.
no more errors.

Code:

#!/usr/bin/perl -w
use strict;
 #open NEWCOMM , "/home/tops11/wjohnson/newcomm.ibfarm100:localtick";
 open NEWCOMM, "newcomm_stat -n localtick ibmis100 |";
  while (<NEWCOMM>) {
          ###my @arry  = split (/\s+/ , $_);
            my @arry  = split (' ' , $_);
            foreach my $line(@arry){
                my $sliced_newcomm = $arry[1];
                if ($sliced_newcomm =~ /\(\w+@(\w+)/){
                    my $newcomm_hosts =  $1;
                    print "$newcomm_hosts\n";
                    sleep 1;
                }
          }
  }



All times are GMT -5. The time now is 02:29 PM.