Hi,

I have a file.txt, I need to enter the data in string. Currently my code looks like this,

#!/usr/bin/perl

open myfiles, "file.txt" or die "couldn't find file: $!";

while (<myfiles>){
   $string .= $_;     
}     
print $string;
close(myfiles);

The output when I print the string is like

hello.c
work.c
office.c

Is it possible to get the output of the string ot be in 1 line with spaces such as

hello.c work.c office.c

If there is a was which can place the data in the string initially like this or if there is a way I can manipulate the string it will be really helpful.

Thanks,
Rahul

In your loop that builds your string, replace the following statement: $string .= $_; with these:

chomp; #Removes trailing new-line character from $_
    $string .= "$_ "; # "$_ followed by one space"

http://perldoc.perl.org/functions/chomp.html

Hi,

I have a file.txt, I need to enter the data in string. Currently my code looks like this,

#!/usr/bin/perl

open myfiles, "file.txt" or die "couldn't find file: $!";

while (<myfiles>){
   $string .= $_;     
}     
print $string;
close(myfiles);

The output when I print the string is like

hello.c
work.c
office.c

Is it possible to get the output of the string ot be in 1 line with spaces such as

hello.c work.c office.c

If there is a was which can place the data in the string initially like this or if there is a way I can manipulate the string it will be really helpful.

Thanks,
Rahul

Thanks, that works perfectly but now I have another question.... $string has a lot of file names stored in it such as hello.c why.c time.c office.c work.c etc. The length of this file is about 30000 characters. I want to make sets of about 7000 characters and I want to make sure that I always have a complete file name at the end so i cant have hello.c why.c ti (it should always have the .c extension). I'm not sure how to do this, any help will be appreciated.

Thanks once again,
Rahul

Interesting. I have to leave my computer now but I'll try and have a look at this tomorrow.

KK thanks, ill also try my luck with some for loops.

You could do something like the following:

#!/usr/bin/perl
use strict;
use warnings;
my $string;
my $desired_string_length = 25; #For testing. You can change this to 7000 later.
my $strcount; #Count the separate strings printed

open my $fh, "/home/david/Programming/Perl/cnames.txt" or die "couldn't find file: $!";

while (<$fh>){
    chomp; #Removes trailing new-line character from $_
    $string .= "$_ "; # "$_ followed by one space"
    if (length($string) > $desired_string_length) {
        print "$string\n";
        $string = ''; #Reset string to empty
        $strcount++; #Add one to counter
    }
}     
if (length($string) > 0) {#If any data remains in string
    print $string;
    $strcount++;
}
close $fh;

Thanks, that works perfectly but now I have another question.... $string has a lot of file names stored in it such as hello.c why.c time.c office.c work.c etc. The length of this file is about 30000 characters. I want to make sets of about 7000 characters and I want to make sure that I always have a complete file name at the end so i cant have hello.c why.c ti (it should always have the .c extension). I'm not sure how to do this, any help will be appreciated.

Thanks once again,
Rahul

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.