Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

spliting up an array 1

Status
Not open for further replies.

Stiddy

IS-IT--Management
Dec 5, 2001
781
US
I have the following which is not doing what I want it to do as usual. I only want to get the row of hostnames. Also I do not need the first row of Region,Disp,FLAGS,Port.etc but have no idea how to get ride of it. Any help would be appreciated.

#!d:\\perl\\bin\\perl -w

open (ODADMIN , "<oadmin_odlist_input.log") || die;
while(<ODADMIN>){
@input=<ODADMIN>;
print $input[7];
}
close (INFILE);


______oadmin_odlist_input.log_______
Region Disp Flags Port IPaddr Hostname(s)
1017389042 1 ct- 94 10.10.10.1 server1.bob.com,server1
2 ct- 94 10.10.10.2 server2.bob.com
3 ct- 94 10.10.10.3 server3.bob.com
174 ct- 94 10.10.10.4 server4.bob.com
175 ct- 94 10.10.10.5 server5.bob.com
181 -t- 94 10.10.10.6 server6.bob.com
183 ct- 94 10.10.10.7 server7.bob.com
184 ct- 94 10.10.10.8 server8.bob.com
185 -t- 94 10.10.10.9 server9.bob.com
186 ct- 94 10.10.10.10 server10.bob.com
416 ct- 94 10.10.10.11 server11.bob.com
661 ct- 94 10.10.10.12 server12.bob.com
762 ct- 94 10.10.10.13 server13.bob.com
798 ct- 94 10.10.10.14 server14.bob.com

HTH - Stiddy
 
Code:
while (<OADMIN>) {
   print (split /\s+/,$_)[-1];
}

Split splits the array with it's first param - /\s+/, the second param is what it splits. It returns an array from which we print the last element with -1.

Corwin
 
Running the following I get:

open (ODADMIN , "<oadmin_odlist_input.log") || die;
while(<ODADMIN>){
print (split /\s+/,$_)[-1];
}
close (ODADMIN);


print (...) interpreted as function at D:\scripts\Endpoint_Check\Daily_Endpoint_
Check.pl line 13.
syntax error at D:\scripts\Endpoint_Check\Daily_Endpoint_Check.pl line 13, near
")["
Execution of D:\scripts\Endpoint_Check\Daily_Endpoint_Check.pl aborted due to co
mpilation errors.

HTH - Stiddy
 
Uf
Code:
print ((split /\s+/,$_)[-1]);

Need the brackets to interpret split as part of what print prints. :)


Corwin
 
I get:

Hostname(s)server1.bob.com,server1server2.bob.comserver3.bob.comserver4.bob.coms
erver5.bob.comserver6.bob.comserver7.bob.comserver8.bob.comserver9.bob.comserver
10.bob.comserver11.bob.comserver12.bob.comserver13.bob.comserver14.bob.com

How would i omit 'Hostname(s)' and also how do I get a carage return after each server name?

HTH - Stiddy
 
What I am trying to do is get the server hostnames out of this array and later I will have another array full of workstation hostanmes. A workstation might have a name like:

abcdwk1

I am gonna try to get the first four characters out of the hostname like

$site = substr($wks,0,4);

The later on in the script I am gonna try to compare the servers in the oadmin_odlist_input.log, after getting each one of the servers first four characters and match them to a workstations first four characters. Thats if you were even interested in knowing that....

HTH - Stiddy
 
Code:
my $flag = 1;
while (<OADMIN>) {
   print (split /\s+/,$_)[-1],"\n") if $flag;
   undef $flag;
}

or
Code:
my @file = <OADMIN>;
for (1 .. $#file) { 
     print (split /\s+/,$_)[-1],"\n");
}


Corwin
 
I get the same error on each:

print (...) interpreted as function at D:\scripts\Endpoint_Check\Daily_Endpoint_
Check.pl line 12.
syntax error at D:\scripts\Endpoint_Check\Daily_Endpoint_Check.pl line 12, near
")["
Execution of D:\scripts\Endpoint_Check\Daily_Endpoint_Check.pl aborted due to co
mpilation errors.

HTH - Stiddy
 
Code:
     print ((split /\s+/,$_)[-1],"\n") unless $flag;

Code:
for (1 .. $#file) {
        print ((split /\s+/,$file[$_])[-1],"\n");
}

hurry and should go

Corwin
 
with:

open (ODADMIN , "<oadmin_odlist_input.log") || die;
my $flag = 1;
my @file = <OADMIN>;
while (<OADMIN>) {
for (1 .. $#file) {
print ((split /\s+/,$_)[-1],"\n") unless $flag;
}
}

close (ODADMIN);


I get:

readline() on unopened filehandle OADMIN at D:\scripts\Endpoint_Check\Daily_Endp
oint_Check.pl line 11.
readline() on unopened filehandle OADMIN at D:\scripts\Endpoint_Check\Daily_Endp
oint_Check.pl line 12.


HTH - Stiddy
 
#!d:\\perl\\bin\\perl -w
open (ODADMIN , "<oadmin_odlist_input.log") || die;
my $flag = 1;
while (<ODADMIN>) { #ODADMIN was spelled wrong#####
print ((split /\s+/,$_)[-1],"\n") unless $flag;
undef $flag;
}
close (ODADMIN);



Got it....thanks alot Corwin


HTH - Stiddy
 
You need to realise that reading a file "line-by-line" with "while(<OADMIN>)" is completely incompatible with slurping the file to an array using "@file = <OADMIN>".
You can't use both and expect it to make any sense.
I suggest you lose the slurp and just read the records line by line, splitting up the fields and "push"ing the field you want onto an array. That way, after the loop you'll have the array populated as you wanted.


Trojan.
 
mactonio. This to me a min to see. ODADMIN and OADMIN look so similar. Thanks for catching it also..

HTH - Stiddy
 
OK Trojan. I will give it a go.

HTH - Stiddy
 
I wrote a quick example for you:
Code:
#!/usr/bin/perl -w
use strict;

my @array = ();
<DATA>; # Reject first row
m/\s+(\S+)\s*$/ and push @array, $1 while(<DATA>);
print "[",join("][",@array),"]\n";
__DATA__
Region           Disp  Flags  Port            IPaddr   Hostname(s)
1017389042          1    ct-    94     10.10.10.1   server1.bob.com,server1
                    2    ct-    94     10.10.10.2   server2.bob.com
                    3    ct-    94    10.10.10.3   server3.bob.com
                  174    ct-    94      10.10.10.4   server4.bob.com
                  175    ct-    94    10.10.10.5   server5.bob.com
                  181    -t-    94     10.10.10.6   server6.bob.com
                  183    ct-    94    10.10.10.7   server7.bob.com
                  184    ct-    94      10.10.10.8   server8.bob.com
                  185    -t-    94      10.10.10.9   server9.bob.com
                  186    ct-    94     10.10.10.10   server10.bob.com
                  416    ct-    94    10.10.10.11   server11.bob.com
                  661    ct-    94     10.10.10.12   server12.bob.com
                  762    ct-    94      10.10.10.13   server13.bob.com
                  798    ct-    94      10.10.10.14   server14.bob.com


Trojan.
 
Code:
[b]#!/usr/bin/perl[/b]

open (ODADMIN, "< oadmin_odlist_input.log");

while (<ODADMIN>) {
  m/([^\s]+)$/;
  print "$1\n";
}

close ODADMIN;


Kind Regards
Duncan
 
Sorry Trojan! Didn't see you were already on the case... ;-)


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top