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: Intermediate:
change join command and add counter

 



tifrah2008
New User

May 24, 2010, 5:27 AM

Post #1 of 2 (1258 views)
change join command and add counter Can't Post

hi, i have the following code :


Code
open(PARAM, "+<$Params"); # open for input 
my(@lines) = <PARAM>;
my %APPL_NAMES = ();
my %HOSTNAMES = ();
seek PARAM,0,0;

for my $line(@lines)
{
# chomp;
my ( $UNITNAME, $APPL_NAME, $HOSTNAME ) = split /\;/, $line;
$APPL_NAMES{$UNITNAME}{$APPL_NAME} ++;
$HOSTNAMES{$UNITNAME}{$HOSTNAME} ++;

}


truncate $Params,0;
for my $UNITNAME ( sort keys %APPL_NAMES ) {
my @APPL_NAMES = sort keys %{ $APPL_NAMES{$UNITNAME} };
my @HOSTNAMES = sort keys %{ $HOSTNAMES{$UNITNAME} };
print PARAM join( ';', $UNITNAME, join( ',', @APPL_NAMES ), join( ',', @HOSTNAMES ) ), "\n";
}
close(PARAM);
}


now, i want also to print to the file the number of hostnames per $UNITNAME. how can i do it using this join ?

Thank You


BillKSmith
Veteran

May 24, 2010, 2:49 PM

Post #2 of 2 (1233 views)
Re: [tifrah2008] change join command and add counter [In reply to] Can't Post

The short answer is:


Code
  

print PARAM join( ';', $UNITNAME, join( ',', @APPL_NAMES ), join( ',', @HOSTNAMES ) ), ',', scalar keys @HOSTNAMES, "\n";





however, I would prefer to see this long statement factored into more logical pieces.


Code
  

my $appl_names = join q(,), @APPL_NAMES;

my $host_names = join q(,), @HOSTNAMES;

print PARAM

$UNITNAME, q(;),

$appl_names, q(,),

$host_names, q(,),

scalar @HOSTNAMES,

qq(\n)

;



The use of the q functions is not really necessary, but it does help to distinguish between characters to be output and the syntax of the join and print statements.
Good Luck,
Bill

 
 


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

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