WaltSteadman
MIS
Good day all,
I am fairly new to Perl (but learning quickly). I am sure my initial code is clunky and I will take suggestions for improvement on that but for this issue I am trying to create a script that will look at a file with a few thousand lines and it will pass only certain lines to an array that will be passed to another array and eventually once parsed down it will print a list. I have searched hi and low and I am probably just using the wrong search string for this but what I am trying to do is as it evaluates wether to pass the line on to a array. The criteria I am trying to weed out are:
no items that are numbers
no items that are less that 4 characters
no items that are more than 4 characters
no duplicate items
below in CURRENT OUTPUT you will see what output I get now and next to each output I put a note if it should remain or not. I am just a bit confused on the syntax for checking wether to pass to the array I have included the current code I am using as well as a sample of the file I am using to get information from. I would appreciate any assistance.
============ CURRENT OUTPUT ==================
Fred (Good pass to array)
Fred (Duplicate DO NOT PASS)
Boba (Good pass to array)
Boba (Duplicate DO NOT PASS)
Bobanna (Longer than 4 characters DO NOT PASS)
3456 (Numeric DO NOT PASS)
2345 (Numeric DO NOT PASS)
Pete (Good pass to array)
Toma (Good pass to array)
Toma (Duplicate DO NOT PASS)
Tom (Shorter than 4 characters DO NOT PASS)
Tom (Shorter than 4 characters DO NOT PASS)
======== DESIRED OUTPUT============
Fred
Boba
Pete
Toma
=======SAMPLE OF FILE========
set address "Group1" "boba" a.b.c.147 255.255.255.255
set address "Group1" "boba" a.b.c.26 255.255.255.255
set address "Group1" "boba" a.b.c.148 255.255.255.255
set address "Group1" "Fred" a.b.c.128 255.255.255.192
set address "Group2" "Joea" a.b.c.40 255.255.255.248
set address "Group2" "Fred" a.b.c.124 255.255.255.255
set address "Group2" "Pete" a.b.c.125 255.255.255.255
set address "Group2" "Stev" a.b.27.0 255.255.255.0
set address "Group3" "Pete" a.b.7.48 255.255.255.248
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "3456-lan" a.b.c.d 255.255.255.0
set address "Group3" "2345-lan" a.b.c.d 255.255.255.0
set address "Group3" "Pete-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0
=============CURRENT CODE===============
1 #! /usr/bin/perl
2 use warnings;
3 use strict;
4 my $testing;
5 my @first_array;
6 my $first_array;
7 my @second_array;
8 my $second_array;
9 my @third_array;
10 my $third_array;
11 my @fourth_array;
12 my $fourth_array;
13
14 open (FWFILE, "/bin/wally/testdata");
15 while (<FWFILE>)
16 { #a
17 @first_array = split / /;
18 if (exists $first_array[1] && $first_array[1] =~ /address/)
19 { #b
20 if (exists $first_array[2] && $first_array[2] =~ /Group3/)
21 { #c
22 if (exists $first_array[3] && $first_array[3] =~ /lan/)
23 { #d
24 @second_array = split /-/, $first_array[3];
25 { #e
26 @third_array = split /\"/, $second_array[0];
27 { #f
28 print $third_array[1];
29 print "\n";
30 } #End f
31 } # End e
32 } # End d
33 } # End c
34 } # End b
35 } #End a
36
37
38 close (FWFILE);
=======SAMPLE OF FILE========
set address "Group1" "boba" a.b.c.147 255.255.255.255
set address "Group1" "boba" a.b.c.26 255.255.255.255
set address "Group1" "boba" a.b.c.148 255.255.255.255
set address "Group1" "Fred" a.b.c.128 255.255.255.192
set address "Group2" "Joea" a.b.c.40 255.255.255.248
set address "Group2" "Fred" a.b.c.124 255.255.255.255
set address "Group2" "Pete" a.b.c.125 255.255.255.255
set address "Group2" "Stev" a.b.27.0 255.255.255.0
set address "Group3" "Pete" a.b.7.48 255.255.255.248
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "3456-lan" a.b.c.d 255.255.255.0
set address "Group3" "2345-lan" a.b.c.d 255.255.255.0
set address "Group3" "Pete-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0
I am fairly new to Perl (but learning quickly). I am sure my initial code is clunky and I will take suggestions for improvement on that but for this issue I am trying to create a script that will look at a file with a few thousand lines and it will pass only certain lines to an array that will be passed to another array and eventually once parsed down it will print a list. I have searched hi and low and I am probably just using the wrong search string for this but what I am trying to do is as it evaluates wether to pass the line on to a array. The criteria I am trying to weed out are:
no items that are numbers
no items that are less that 4 characters
no items that are more than 4 characters
no duplicate items
below in CURRENT OUTPUT you will see what output I get now and next to each output I put a note if it should remain or not. I am just a bit confused on the syntax for checking wether to pass to the array I have included the current code I am using as well as a sample of the file I am using to get information from. I would appreciate any assistance.
============ CURRENT OUTPUT ==================
Fred (Good pass to array)
Fred (Duplicate DO NOT PASS)
Boba (Good pass to array)
Boba (Duplicate DO NOT PASS)
Bobanna (Longer than 4 characters DO NOT PASS)
3456 (Numeric DO NOT PASS)
2345 (Numeric DO NOT PASS)
Pete (Good pass to array)
Toma (Good pass to array)
Toma (Duplicate DO NOT PASS)
Tom (Shorter than 4 characters DO NOT PASS)
Tom (Shorter than 4 characters DO NOT PASS)
======== DESIRED OUTPUT============
Fred
Boba
Pete
Toma
=======SAMPLE OF FILE========
set address "Group1" "boba" a.b.c.147 255.255.255.255
set address "Group1" "boba" a.b.c.26 255.255.255.255
set address "Group1" "boba" a.b.c.148 255.255.255.255
set address "Group1" "Fred" a.b.c.128 255.255.255.192
set address "Group2" "Joea" a.b.c.40 255.255.255.248
set address "Group2" "Fred" a.b.c.124 255.255.255.255
set address "Group2" "Pete" a.b.c.125 255.255.255.255
set address "Group2" "Stev" a.b.27.0 255.255.255.0
set address "Group3" "Pete" a.b.7.48 255.255.255.248
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "3456-lan" a.b.c.d 255.255.255.0
set address "Group3" "2345-lan" a.b.c.d 255.255.255.0
set address "Group3" "Pete-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0
=============CURRENT CODE===============
1 #! /usr/bin/perl
2 use warnings;
3 use strict;
4 my $testing;
5 my @first_array;
6 my $first_array;
7 my @second_array;
8 my $second_array;
9 my @third_array;
10 my $third_array;
11 my @fourth_array;
12 my $fourth_array;
13
14 open (FWFILE, "/bin/wally/testdata");
15 while (<FWFILE>)
16 { #a
17 @first_array = split / /;
18 if (exists $first_array[1] && $first_array[1] =~ /address/)
19 { #b
20 if (exists $first_array[2] && $first_array[2] =~ /Group3/)
21 { #c
22 if (exists $first_array[3] && $first_array[3] =~ /lan/)
23 { #d
24 @second_array = split /-/, $first_array[3];
25 { #e
26 @third_array = split /\"/, $second_array[0];
27 { #f
28 print $third_array[1];
29 print "\n";
30 } #End f
31 } # End e
32 } # End d
33 } # End c
34 } # End b
35 } #End a
36
37
38 close (FWFILE);
=======SAMPLE OF FILE========
set address "Group1" "boba" a.b.c.147 255.255.255.255
set address "Group1" "boba" a.b.c.26 255.255.255.255
set address "Group1" "boba" a.b.c.148 255.255.255.255
set address "Group1" "Fred" a.b.c.128 255.255.255.192
set address "Group2" "Joea" a.b.c.40 255.255.255.248
set address "Group2" "Fred" a.b.c.124 255.255.255.255
set address "Group2" "Pete" a.b.c.125 255.255.255.255
set address "Group2" "Stev" a.b.27.0 255.255.255.0
set address "Group3" "Pete" a.b.7.48 255.255.255.248
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0
set address "Group3" "3456-lan" a.b.c.d 255.255.255.0
set address "Group3" "2345-lan" a.b.c.d 255.255.255.0
set address "Group3" "Pete-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0
set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0