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:
delete new line from hash value

 



nieselfriem
New User

Dec 3, 2014, 4:33 AM

Post #1 of 2 (1520 views)
delete new line from hash value Can't Post

Hello,

i have created a hash in this way

Code
@tcmcts = ("1t","2t",...); 
my @dirs = `find /vol1/ -maxdepth 1 -type d -name 'tomcat[1-9]' -or -name 'apache-tomcat-7.0.50'`;
@inputpathes{@tmcts}=@dirs;

"1t" => "/vol1/tomcat1"
"2t" => "/vol1/tomcat2"

I use an loop to get the values from this hash in this way and i will concat a string to the value:


Code
while(($key, $value) =each(%dirhash)) { 
print $key.", ".$value."\n";
$logdir = "$values"."/logs";


But the proble that the logdir is


Code
/vol1/tomcat1 

/log

insteat of

Code
/vol1/tomcat1/log


What can i do to create an valid path string?

greetings niesel


(This post was edited by nieselfriem on Dec 3, 2014, 4:35 AM)


Zhris
Enthusiast

Dec 3, 2014, 5:41 AM

Post #2 of 2 (1515 views)
Re: [nieselfriem] delete new line from hash value [In reply to] Can't Post

Hi,

Firstly, there are mistakes in the code you provided and discrepencies between code and output:

- you define @tcmcts, but you use @tmcts.
- your hash output shows no newlines in values, but I'm pretty certain newlines is the issue.
- $values is not defined anywhere.
- you concatenate /logs but your output shows /log.
- your output shows two newlines but I assume theres just one.

However, I believe your solution is to chomp @dirs after you have assigned it to remove the newlines:


Code
my @dirs = `...`; 
chomp @dirs;


Here is my rough implementation of your code for reference:


Code
use strict; 
use warnings;
use File::Spec::Functions;

my %hash;
my @keys = ( '1t' );
my @vals = `find -type d`;
chomp @vals;
@hash{@keys} = @vals;

while( my ( $key, $value ) = each %hash )
{
my $logdir = catfile( $value, 'logs' ); # $value . '/logs'
print $logdir, "\n";
}


Regards,

Chris


(This post was edited by Zhris on Dec 3, 2014, 5:48 AM)

 
 


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

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