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

Hi Not sure where to begin in describing my problem so I'll start with the code
open(INFILE, "iftoportchop"); while(<INFILE>){ @if2port = split /:/,$_; if ($if2port[0] =~ /([1-9]\.\d+)/){ $numvar1 = $1; } open(INFILE2,"portnameok"); LINE2: while(<INFILE2>){ @pnamok = split /:/,$_; if ($pnamok[0] =~ /([1-9]\.\d+)/){ $numvar2 = $1; next LINE2 if $numvar2 > $numvar1; if ($numvar1 eq $numvar2){ open(OUTFILE4, ">> swapnum"); s/$pnamok[0]/$if2port[2]/; print OUTFILE4; } } } }
The above works and I end up with the values swapped, however when the values are swapped a "carriage return" is inserted so the final output ends up spread across two lines, I of course need it to be on one line. Here is an example of my source data.
"iftoportchop" .portIfIndex.3.1 : INTEGER: 377 "portnameok" .portName.3.1 : DISPLAY STRING- (ascii): HX06S24E-CHASSIS1
Below is a sample of the output that I get
377 : DISPLAY STRING- (ascii): HX06S24E-CHASSIS1
Whilst looking though Q&A I found a reference to
$var =~ tr/\n/ /;
But this has the effect of removing all of the hard returns, which is not what I want. Any Ideas on resolving this problem will be most appreciated. Thanks

Replies are listed 'Best First'.
Re: swapping values between two files
by saintmike (Vicar) on Feb 18, 2005 at 20:51 UTC
    Couple of comments on the code:
    • Variable names like numvar2 and OUTFILE4 don't tell the reader what's stored in them or what the purpose of the filehandle is. Better use descriptive names.
    • You're scanning through all lines of portnameok every time you're reading a line from iftoportchop. That's probably unnecessarily expensive. Can you read in iftoportchop once and store everything you need in a data structure instead or is iftoportchop prohibitively large?
    • The next command doesn't really accomplish anything in this case. Did you mean to use last instead?
Re: swapping values between two files
by RazorbladeBidet (Friar) on Feb 18, 2005 at 20:35 UTC
    After the declaration of each while loop add a

    chomp

    (e.g.)
    while (<FILE>) { chomp; ... }
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
      Make that only for the first while loop. They still want the newline from the second file.

      Caution: Contents may have been coded under pressure.
      Hi
      Thanks for your quick reply, but adding chomp after the declaration of each while loop has the same effect as adding after $_ =~ tr/\n/ /; after the values have been swapped.
      In that is removes all of the carriage returns and I end up with a continious stream of text.
      However your suggestion pointed the way to the solution, I've added chomp after the declaration of the second while loop and that appears to have fixed my problem.
      A thousands thanks
        In that is removes all of the carriage returns and I end up with a continious stream of text.

        yes, which is why you also have to change the print to print OUTFILE4 $_, "\n";