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
 
Point taken!!!

... however, strangely enough, inversely actually makes more sense in my weird head!


Kind Regards
Duncan
 
It's actually a VERY useful trick for dealing with unicode.
For example, to match an upper or lower case letter allowing for locale you could use:
Code:
[^\W0-9_]
Cool huh?!


Trojan.
 
With something like this. How would I compare the two list then:

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

open (DATA , "<oadmin_odlist_input.log") || die;
open (WKS , "<wks.log") || die;
#use strict;

my @array = ();
<DATA>; # Reject first row
m/\s+(\S+)\s*$/ and push @array, $1 while(<DATA>);
foreach $_ (@array) {
$serversitecode = substr($_,0,4);
print "$serversitecode\n";
}

my @WKS = ();
while (<@WKS>) {
$wkssitecode = substr($_,0,4);
if ($wkssitecode eq $serversitecode) {print "Yeah folks we have a match\n"} else {print "Not a match Gentleman\n";}
}

___WKS____
BBBBwks1
BBBBwks2
FFFFwks8
IIIIwks23
JJJJwks909


___DATA____
Region Disp Flags Port IPaddr Hostname(s)
1017389042 1 ct- 94 10.10.10.1 AAAAer1.bob.com,AAAAer1
2 ct- 94 10.10.10.2 BBBBer2.bob.com
3 ct- 94 10.10.10.3 CCCCer3.bob.com
174 ct- 94 10.10.10.4 DDDDer4.bob.com
175 ct- 94 10.10.10.5 EEEEer5.bob.com
181 -t- 94 10.10.10.6 FFFFer6.bob.com
183 ct- 94 10.10.10.7 GGGGer7.bob.com
184 ct- 94 10.10.10.8 HHHHer8.bob.com
185 -t- 94 10.10.10.9 IIIIer9.bob.com
186 ct- 94 10.10.10.10 JJJJer10.bob.com
416 ct- 94 10.10.10.11 KKKKer11.bob.com
661 ct- 94 10.10.10.12 LLLLer12.bob.com
762 ct- 94 10.10.10.13 MMMMer13.bob.com
798 ct- 94 10.10.10.14 NNNNer14.bob.com

HTH - Stiddy
 
Oh, first of all $serversitecode is only a known varible inside of that loop, so I guess I would have to use 'return' to make that varible available to the rest of the script. Is that correct?

HTH - Stiddy
 
So let me get this straight.
You want to match the first four characters of WKS against the first for characters of the oadmin servername?
When you get a match, what do you want to output?
Then entire record from the oadmin file? Just the servername?


Trojan.
 
ODADMIN will always be small and `word for word` just like ___DATA____ in the example. _WKS_ data could be thousands of workstations. The hole script centers around the workstations. I am writing this for another Tivoli process. I need to get all of the workstations into an array and then do a foreach loop. I will run a test on a workstation like 'wep AAAAwks1 status' which basically test the abilty for the Tivoli server to communicate with a workstation. If the test works then great, if not its broke and I've got problems. So knowing this I have to run a command like
`wep AAAAwks1 AAAAer1 fix_this_machine`; #to fix the machine

So i was trying to loop though the list of workstations and perform the `wep` test. if it is broke then get the server name out of the array and pass that argument to the command `wep AAAAwks1 AAAAer1 fix_this_machine`; You will notice that the first four characters of the workstation and server hostname is the same. Knowing that I thought I could somehow use `substr` to compare the to values to figure out the appropriate server to pass to the command. An array maynot be the best way to do this.

HTH - Stiddy
 
so i will endup doing some thing

$test = `wep a_workstation status`;
if ($test=~/alive/){print "good, its not broke then";
} else {
`wep AAAAwks1 AAAAer1 fix_this_machine`;
print "we fixed it, greate job\n";
}

I dont know anything about hashes I hate to say. I will give it a go. I have tried before unsuccessfully. Thanks.

HTH - Stiddy
 
OK, try this:
Code:
#!/usr/bin/perl -w
use strict;

my %lookup = ();
<DATA>; # Reject first row
m/\s+(\S+)\s*$/ and $lookup{substr($1,0,4)} = $1 while(<DATA>);

while(<>) {
  chomp;
  my $key = substr($_,0,4);
  print "[",$_,"][",$lookup{$key},"]\n"
    if(exists $lookup{$key});
}

__DATA__
Region           Disp  Flags  Port            IPaddr   Hostname(s)
1017389042          1    ct-    94     10.10.10.1   AAAAer1.bob.com,AAAAer1
                    2    ct-    94     10.10.10.2   BBBBer2.bob.com
                    3    ct-    94    10.10.10.3   CCCCer3.bob.com
                  174    ct-    94      10.10.10.4   DDDDer4.bob.com
                  175    ct-    94    10.10.10.5   EEEEer5.bob.com
                  181    -t-    94     10.10.10.6   FFFFer6.bob.com
                  183    ct-    94    10.10.10.7   GGGGer7.bob.com
                  184    ct-    94      10.10.10.8   HHHHer8.bob.com
                  185    -t-    94      10.10.10.9   IIIIer9.bob.com
                  186    ct-    94     10.10.10.10   JJJJer10.bob.com
                  416    ct-    94    10.10.10.11   KKKKer11.bob.com
                  661    ct-    94     10.10.10.12   LLLLer12.bob.com
                  762    ct-    94      10.10.10.13   MMMMer13.bob.com
                  798    ct-    94      10.10.10.14   NNNNer14.bob.com

You'll need to feed the large file in on stdin (maybe as a parameter on the command line).

Let me know if this does what you want.



Trojan.
 
The idea of this script is for it to run fully automated. So command line input is not an option other than another script which creates a file that can then be read in as a command line argument. It will kick of as a windows scheduled task on a daily basis and look for workstations that have a problem within tivoli and attempt to correct the issue. Thanks for your help. I will try and see if I can incorporate your new code. Get back tommorrow. Boss wants me to travel to another customer site. Thanks again for all whom are helping.

HTH - Stiddy
 
Sorry, I didn't realise that you were running windoze.
No problem though, you could easily open a file and use a filehandle as you did before.
Run what I've given you and see if it is near to what you want.


Trojan.
 
[cite]
reading a file "line-by-line" with "while(<OADMIN>)" is completely incompatible with slurping the file to an array using "@file = <OADMIN>".
[/cite]
Where this happens?
Also why is better using regexpr instead of split?

Corwin
 
Corwinsw,
The original script that Stiddy supplied did this:
Code:
open (ODADMIN , "<oadmin_odlist_input.log") || die;
[red]while(<ODADMIN>){
@input=<ODADMIN>;[/red]
print $input[7];
}
close (INFILE);

Split uses a regex as it's first parameter. Sometimes split is more obvious (like breaking up comma separated fields into an array).


Trojan.
 
No I meant why do you use:
Code:
m/\s+(\S+)\s*$/ and push @array, $1 while(<DATA>);
instead of split, is there any advantage?

Corwin
 
Yes, I only want one field (the last one) and that regex gives me exactly that.
Split would give me ALL the fields and I would then have to "throw away" all but the last.


Trojan.
 
you could use an array slice with split to get just the last field, but maybe that silently throws away the rest of the fields in the background.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top