CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Filtering Array based on field

 



frist44
User

Jan 9, 2009, 12:01 PM

Post #1 of 14 (1661 views)
Filtering Array based on field Can't Post

I have the output of a command streamed into a variable. I removed the first three lines, but I would like to keep only lines that have "0" for the Weight field.

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn
TCP 10.10.6.2:80 rr
-> 10.10.6.3:80 Masq 1 0 0
TCP 10.10.7.2:80 rr
-> 10.10.7.3:80 Masq 0 0 0
TCP 10.10.8.2:80 rr
-> 10.10.8.3:80 Masq 1 0 0
TCP 10.10.9.2:80 rr
-> 10.10.9.3:80 Masq 0 0 0

So ideally i should end up with:

-> 10.10.7.3:80 Masq 0 0 0
-> 10.10.9.3:80 Masq 0 0 0

What is the best way?


FishMonger
Veteran / Moderator

Jan 9, 2009, 12:43 PM

Post #2 of 14 (1659 views)
Re: [frist44] Filtering Array based on field [In reply to] Can't Post

See
perldoc -f grep


(This post was edited by FishMonger on Jan 9, 2009, 12:43 PM)


frist44
User

Jan 9, 2009, 12:49 PM

Post #3 of 14 (1656 views)
Re: [FishMonger] Filtering Array based on field [In reply to] Can't Post

I read it and it didn't really help much. Can you give some other direction?


FishMonger
Veteran / Moderator

Jan 9, 2009, 12:54 PM

Post #4 of 14 (1655 views)
Re: [frist44] Filtering Array based on field [In reply to] Can't Post

Post the code you're using that retrieves and stores the data.


frist44
User

Jan 9, 2009, 2:22 PM

Post #5 of 14 (1651 views)
Re: [FishMonger] Filtering Array based on field [In reply to] Can't Post


Code
my $read; 
my $result;
my @lines;

open(OUTPUT, "ipvsadm -L -n|");
@lines = <OUTPUT>;
close(OUTPUT);

print @lines;



FishMonger
Veteran / Moderator

Jan 9, 2009, 2:57 PM

Post #6 of 14 (1649 views)
Re: [frist44] Filtering Array based on field [In reply to] Can't Post


Code
my @output; 
open(OUTPUT, "ipvsadm -L -n|") or die "unable to open pipe to ipvsadm $!";

while(my $line = <OUTPUT>) {
push @output, $line if $line =~ /Masq 0/;
}
print $_ for @output;



FishMonger
Veteran / Moderator

Jan 9, 2009, 3:01 PM

Post #7 of 14 (1647 views)
Re: [frist44] Filtering Array based on field [In reply to] Can't Post


Code
open(OUTPUT, "ipvsadm -L -n|") or die "unable to create pipe to ipvsadm $!"; 
my @output = grep{ /Masq 0/ } <OUTPUT>;
print $_ for @output;



frist44
User

Jan 9, 2009, 3:08 PM

Post #8 of 14 (1645 views)
Re: [FishMonger] Filtering Array based on field [In reply to] Can't Post


In Reply To

Code
open(OUTPUT, "ipvsadm -L -n|") or die "unable to create pipe to ipvsadm $!"; 
my @output = grep{ /Masq 0/ } <OUTPUT>;
print $_ for @output;



"my" variable @output masks earlier declaration in same scope at ./script.pl

?


FishMonger
Veteran / Moderator

Jan 9, 2009, 3:14 PM

Post #9 of 14 (1644 views)
Re: [frist44] Filtering Array based on field [In reply to] Can't Post


In Reply To

In Reply To

Code
open(OUTPUT, "ipvsadm -L -n|") or die "unable to create pipe to ipvsadm $!"; 
my @output = grep{ /Masq 0/ } <OUTPUT>;
print $_ for @output;



"my" variable @output masks earlier declaration in same scope at ./script.pl

?

Remove the earlier
my @output;


frist44
User

Jan 9, 2009, 3:32 PM

Post #10 of 14 (1640 views)
Re: [FishMonger] Filtering Array based on field [In reply to] Can't Post

perfect.

Thanks!


frist44
User

Jan 10, 2009, 9:51 AM

Post #11 of 14 (1631 views)
Re: [FishMonger] Filtering Array based on field [In reply to] Can't Post


In Reply To

Code
open(OUTPUT, "ipvsadm -L -n|") or die "unable to create pipe to ipvsadm $!"; 
my @output = grep{ /Masq 0/ } <OUTPUT>;
print $_ for @output;



How do I output the array @output into a scalar?


FishMonger
Veteran / Moderator

Jan 10, 2009, 9:55 AM

Post #12 of 14 (1630 views)
Re: [frist44] Filtering Array based on field [In reply to] Can't Post

perldoc -f join


KevinR
Veteran


Jan 10, 2009, 11:19 AM

Post #13 of 14 (1627 views)
Re: [frist44] Filtering Array based on field [In reply to] Can't Post


In Reply To

In Reply To

Code
open(OUTPUT, "ipvsadm -L -n|") or die "unable to create pipe to ipvsadm $!"; 
my @output = grep{ /Masq 0/ } <OUTPUT>;
print $_ for @output;



How do I output the array @output into a scalar?


To convert the array to a scalar put it in double-quotes and assign it to a scalar:

my $var = "@array";

or to simply print it as a scalar:

print "@output";

or use join() as suggested
-------------------------------------------------


FishMonger
Veteran / Moderator

Jan 10, 2009, 11:49 AM

Post #14 of 14 (1624 views)
Re: [KevinR] Filtering Array based on field [In reply to] Can't Post

my $output = join('', grep{ /Masq 0/ } <OUTPUT>);

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives