Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Concat strings from parameter file

by jevaly (Sexton)
on Dec 08, 2009 at 09:23 UTC ( [id://811691]=perlquestion: print w/replies, xml ) Need Help??

jevaly has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
when I run the following code
#! perl -w use strict; use warnings; use Carp; use DBI; use DBD::Oracle qw(:ora_types); my %env_hash; my $os = 'solaris'; my $stringetje; my $temp; my @tempar; my @wkflist; my $PARAMETER; $env_hash{$os} = {parfile => '/ontw/data/powercenter8/PRJ10/BMRK.par', del => 'rm -f ', files => 'wf_BMRK*.out'}; open($PARAMETER, '<', "$env_hash{$os}{parfile}") or croak "\nCould not + open parameterfile $env_hash{$os}{parfile}"; while($temp = <$PARAMETER>){ chomp($temp); @tempar = split /=/, $temp; if($tempar[0] eq "WKFLIST"){ @wkflist = split /,/,$tempar[-1]; } else{ $env_hash{$os}{$tempar[0]} = $tempar[-1]; } } close $PARAMETER; $stringetje = $env_hash{$os}{del} . $env_hash{$os}{LOGDIR} . $env_hash +{$os}{files}; print "\n\n\n\n******************************************************\ +n"; print "\nFirst part of the concatenation: delete command : $env_hash{$ +os}{del}"; print "\nSecond part of the concatenation: path : $env_hash{$os}{LOGDI +R}"; print "\nThird part of the concatenation: file wildcard : $env_hash{$o +s}{files}"; print "\nThe resulting concatenation: $stringetje"; print "\n******************************************************\n\n\n\ +n";


and use the following parameter file BMRK.par:

TIMES=4 LOGDIR=/ontw/log/powercenter8/PRJ10/ TMPDIR=/ontw/data/powercenter8/Tmp/PRJ10 FOLDER=BMRK SERVICE=IS_PRJ10 DOMAIN=Domain_ONT USER=Administrator PASSWORD=********** WKFLIST="wf_BMRK_File2File_Fixed","wf_BMRK_File2File_Fixed_No_Target", +"wf_BMRK_File2File_Delimited","wf_BMRK_File2File_Memory","wf_BMRK_Fil +e2File_CPU" ORAUSER=jamb_007 ORAPSW=********** ORACONNECT=dow02.ont.jon.mg


this renders the following output:
****************************************************** First part of the concatenation: delete command : rm -f Second part of the concatenation: path : /ontw/log/powercenter8/PRJ10/ Third part of the concatenation: file wildcard : wf_BMRK*.out wf_BMRK*.outg concatenation: rm -f /ontw/log/powercenter8/PRJ10/ ******************************************************

However, If I hardcode the $env_hash{$os}{LOGDIR} parameter, as the other $env_hash{$os} parameters the output is the correct:
****************************************************** First part of the concatenation: delete command : rm -f Second part of the concatenation: path : /ontw/log/powercenter8/PRJ10/ Third part of the concatenation: file wildcard : wf_BMRK*.out The resulting concatenation: rm - f /ontw/log/powercenter8/PRJ10/wf_BM +RK*.out ******************************************************

I have absolutely no clue why this behaves as it does. As some parameters are shared, I'd like to keep the parameter file (so hardcoding the parameters which does work is not really an option). Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Concat strings from parameter file
by almut (Canon) on Dec 08, 2009 at 09:36 UTC
    wf_BMRK*.outg concatenation: rm -f /ontw/log/powercenter8/PRJ10/

    Looks like there is a carriage return (\r) at the end of the string in $env_hash{$os}{LOGDIR} (i.e. "/ontw/log/powercenter8/PRJ10/\r") , which makes the terminal's cursor move to the beginning of the line...  (To verify, redirect the output to a file and then hexdump its contents.)


      When I look at the hexadecimal version of the parameter file there is indeed a carriage return ('0D').
      However, I lived in the blissful ignorance that the chomp($temp) would spare me all the nasty newline problems. Apparently it doesn't?
        ...that the chomp($temp) would spare me all the nasty newline problems

        Depends on what's in $/$/ by default holds "\n". On Windows, the :crlf PerlIO layer is responsible for translating "\r\n" to "\n". On Unix, this layer is not active (by default), so in case you have input coming from Windows (via cut-n-paste, or whatever), the carriage returns will survive.

        chomp() will remove only trailing string that corresponds to the current value of $/ ($INPUT_RECORD_SEPARATOR).
        jevaly:

        Yep ... in the Windows world, things can be a bit messy. Many Windows users simply replace chomp $varname; with $varname =~ s/\s+$//; to trim off all trailing whitespace from a variable.

        NOTE: It's not an exact replacement, so if your code expects whitespace at the end of your lines, you'll probably want to adjust $/ to the proper value for your incoming files. For my purposes, removing whitespace from the end of the string works fine in nearly all cases.

        ...roboticus
Re: Concat strings from parameter file
by ph0enix (Friar) on Dec 08, 2009 at 09:49 UTC
    You have to check line endings in your parameter file. It looks like you have mixed combination of \r and \n which chomp() do not remove.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://811691]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found