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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

criteria for passing info to array

Status
Not open for further replies.
Oct 27, 2002
21
US
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
 
Maybe
Code:
while (<FILE>) {
	@tmp = split /\s+/, $_;
	$tmp[3] =~ s/"//g;
	if ($tmp[3] !~ /\d/ && $tmp[3] =~ /^\w{4}$/ && !(grep /$tmp[3]/, @out)) {
		push @out, "$tmp[3]\n";
	}
}

print "@out\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%seen[/blue] = [red]([/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@out[/blue] = [red]([/red][red])[/red][red];[/red]
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
   [black][b]my[/b][/black] [blue]@d[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red])[/red][red];[/red]
   [blue]$d[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red] =~ [red]tr/[/red][purple]"[/purple][red]/[/red][purple][/purple][red]/[/red][red]d[/red][red];[/red]
   [olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/length.html][black][b]length[/b][/black][/url][red]([/red][blue]$d[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red][red])[/red] != [fuchsia]4[/fuchsia] || [blue]$d[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red] =~ [red]/[/red][purple][purple][b]\d[/b][/purple][/purple][red]/[/red] || ++[blue]$seen[/blue][red]{[/red][blue]$d[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red][red]}[/red] > [fuchsia]1[/fuchsia][red])[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@out[/blue],[blue]$_[/blue][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]@out[/blue][red];[/red]	 
[teal]__DATA__[/teal]
[teal]set address "Group1" "boba" a.b.c.147 255.255.255.255[/teal]
[teal]set address "Group1" "boba" a.b.c.26 255.255.255.255[/teal]
[teal]set address "Group1" "boba" a.b.c.148 255.255.255.255[/teal]
[teal]set address "Group1" "Fred" a.b.c.128 255.255.255.192[/teal]
[teal]set address "Group2" "Joea" a.b.c.40 255.255.255.248[/teal]
[teal]set address "Group2" "Fred" a.b.c.124 255.255.255.255[/teal]
[teal]set address "Group2" "Pete" a.b.c.125 255.255.255.255[/teal]
[teal]set address "Group2" "Stev" a.b.27.0 255.255.255.0[/teal]
[teal]set address "Group3" "Pete" a.b.7.48 255.255.255.248[/teal]
[teal]set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255[/teal]
[teal]set address "Group3" "Fred-lan" a.b.c.148 255.255.255.255[/teal]
[teal]set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0[/teal]
[teal]set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0[/teal]
[teal]set address "Group3" "Boba-lan" a.b.c.d 255.255.255.0[/teal]
[teal]set address "Group3" "3456-lan" a.b.c.d 255.255.255.0[/teal]
[teal]set address "Group3" "2345-lan" a.b.c.d 255.255.255.0[/teal]
[teal]set address "Group3" "Pete-lan" a.b.c.d 255.255.255.0[/teal]
[teal]set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0[/teal]
[teal]set address "Group3" "Toma-lan" a.b.c.d 255.255.255.0[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin
++$seen{$d[3]} > 1

Does this check it then increment it or increment it then check it? I'm assuming the former the way it's being used but thought I'd ask.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
increments first then checks

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top