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

This is going to sound stupid, but what the hell.

I have a file that has text in it like this:

dm1w50
5.5.1

What I'm trying to do is place the 5.5.1 on the right side of the dm1w50. Somethink like this:

dm1w50 5.5.1

I think that I should use a while loop until I reach the end of file, but I'm having a hard time writing the matching string and placing the string into a variable.

Can someone please help?
Curtisb -- "Needs help!!!!"
  • Comment on Reading Text from file and reorganization

Replies are listed 'Best First'.
Re: Reading Text from file and reorganization
by chipmunk (Parson) on May 08, 2001 at 00:46 UTC
    Here's one way to do it, by simply chomping the line ending from every other line:
    #!perl -i.bak $toggle = 0; while (<>) { chomp if $toggle ^= 1; print; } print "\n" if $toggle; # if the last line was chomped
    If you wanted, you could use the -p option to make the reading and printing parts automatic:
    #!perl -pi.bak BEGIN { $toggle = 0; } chomp if $toggle ^= 1; END { print "\n" if $toggle; }
Re: Reading Text from file and reorganization
by merlyn (Sage) on May 08, 2001 at 01:07 UTC
Re: Reading Text from file and reorganization
by DrZaius (Monk) on May 08, 2001 at 00:30 UTC
    Try this:
    my @data = <FILE> my @new_data; chomp @data; while(@data) { push @new_data, join(' ', shift(@data), shift(@data)); }
    This is, of course, assuming that your text file contains 'paired lines'.

    There might be a map trick you could use too, but I can't think of how you would do it in 3 minutes :)

Re: Reading Text from file and reorganization
by rchiav (Deacon) on May 08, 2001 at 00:25 UTC
    I'm afraid we're going to need more information:

    Is this text at the top of the file or could it be anywhere in the file?
    Where do you want to concatinate this text? Do you want to write the file back out this way or do you just want to store it internally this way?
    Does the file have any type of standard format?
    What have you written to attempt to do this?
    Can we see the file?

    Rich

      sorry for the lack of information. Here is some more.
      The text is at the top of the file and goes down the page in a single column. So I need for every even entry of the file to be on the right side of the ode entry. Like so:

      dm1w50
      5.5.1

      Should look like this:
      dm1w50 5.5.1

      This should continue until the end of file. I want to write this back to a new file stored in the same location as the old file..
      I;m still trying to decide if I should do this as a while loop or a foreach loop. I'm currently looking through books trying to make my decision.
      The file looks like the example above but just a lot more entries.
      curtisb
        What I'd do is the following..
        #!/usr/bin/perl -w use strict; my @newfile; open (FH, "./testfile"); while (<FH>) { chomp; push @newfile, $_.' '.<FH>; } close FH;
        Here's what's going on. In the while loop I'm getting the 1st line of the file. Then I'm taking the newline character off of it. Then.. I'm taking that line, concatinating it with a space and the next line from the file. NOTE: I'm not stripping the newline character off of that one because you're going to want a new line there :). Then I'm pushing that new string on to an array.

        Now all you have to do is write it out..

        Hope this helps..
        Rich