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: Intermediate:
String manipulation

 



rudasi
Novice

May 20, 2010, 6:56 AM

Post #1 of 17 (2973 views)
String manipulation Can't Post

Hi,

I just recently started perl for a project. At the moment I have a string $allfiles which has filenames such as hello.c work.c school.c time.c etc. There are files with a total length of about 30000 characters. I need to make substrings from this big string with about 8000 characters. The problem is that I have to make sure that each smaller string I have ends with a .c so i cannot just substr the first 8000 cause that could give me a string which ends like tim instead of time.c . Any help will be much appreciated.

Thanks,

Rahul


FishMonger
Veteran / Moderator

May 20, 2010, 7:04 AM

Post #2 of 17 (2971 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

Is there a space or any other character separating the filenames? If so, then simply use the split function.

It's usually better to fix problems at their source. How was $allfiles assigned?

Please post your code and provide more details on what you really need to accomplish.


(This post was edited by FishMonger on May 20, 2010, 7:05 AM)


rudasi
Novice

May 20, 2010, 7:29 AM

Post #3 of 17 (2967 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post

The file names have a space between them. I have a txt files with all the file names, this is how $allfiles was assigned,



Code
  

#!/usr/bin/perl

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



$allfiles="";



while (<myfiles>){

chomp;
$allfiles .= "$_ ";
}



$allfiles looks like hello.c name.c work.c with spaces between the filenames. Im trying to compile a lot of these files but since im using a command prompt I have a limit of about 8100 characters which can be passed. So I want to break the 30000 something character string into a few a different one and compile them indivdually.


FishMonger
Veteran / Moderator

May 20, 2010, 7:57 AM

Post #4 of 17 (2965 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

Why not simply compile each one as you read-in its name?

If you need to use a long string of filenames, then use an array. Prior to adding the next filename, check the length of the string in the array element to determine if it should be added to the current element or the next element.

Every Perl script you write should include these 2 pragmas.

Code
use strict; 
use warnings;


All vars need to be declared with the 'my' keyword.

Always use the 3 arg form of open and a lexical var for the filehandle.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $file = 'testingfile2.txt';
open my $filehandle, '<', $file or die "couldn't open '$file': $!";

my @files;
my $i = 0;

while ( my $filename = <$filehandle> ){

chomp $filename;
$i++ if length($files[$i]) > 8000;
$files[$i] .= "$filename ";
}

close $filehandle or die "failed to close $file";



rudasi
Novice

May 20, 2010, 8:12 AM

Post #5 of 17 (2963 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post

Thanks for reply. Ive added teh 2 pragmas to my script. The problem with compiling them individually is it takes much longer than im allowed. By putting it in a string it decreases this time by a good amount. If i put the file names in an array it will be the same as compiling them individually cause ill have to loop through the array. Im trying to implement a way using a few while loops and if conditions to make sub strings. Is there any character i can check for to see if im at the end of my string? I want to use this in my outer while loop to know when to stop reading the characters. Also is there a way to read each individual character similar to getchar() in c? My basic logic at moment (which has too many flaws) is to check if the string is < 8000. If it is i just compile. If its bigger than 8000 i make a substring of 8000 and i check the last char to see if its a ".", if its a dot i just add the next char to the string which is "c". If its not a dot i keep on adding chars till i get a "." then i add the next char which is a "c". This way ive been able to make the first string to be about 8009 chars but its ends properly with a ".c" and so it will compile. Now im trying to start my second string at 8010 chars and keep on adding about 8000 more chars. Ill work a bit more on the code and post it.


FishMonger
Veteran / Moderator

May 20, 2010, 8:19 AM

Post #6 of 17 (2961 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

Did you test the code I posted? It does exactly what you want.

The array will end up holding a list of strings that are slightly over 8000 characters. All you need to do is loop over the array.


rudasi
Novice

May 20, 2010, 8:35 AM

Post #7 of 17 (2959 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post

Hi, I tried testing it but it stopped didnt compile, the error i get is that,

the "my" variable $file masks earlier declaration in same statement, syntax error at line 7 near open.

Im still really new to perl, if possible could you explain the main parts of the code?

Thanks once again


FishMonger
Veteran / Moderator

May 20, 2010, 8:37 AM

Post #8 of 17 (2957 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

Obviously you altered the code.

Post the code you tested so I can show you what you did wrong.


rudasi
Novice

May 20, 2010, 8:42 AM

Post #9 of 17 (2954 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post


Code
#!/usr/bin/perl use strict; 
use warnings; my $file = 'testingfile2.txt'
open my $filehandle, '<', $file or die "couldn't open '$file': $!"; my @files;
my $i = 0;while ( my $filename = <$filehandle> ){
chomp $filename;
$i++ if length($files[$i]) > 8000;
$files[$i] .= "$filename ";
}close $filehandle or die "failed to close $file";



FishMonger
Veteran / Moderator

May 20, 2010, 8:50 AM

Post #10 of 17 (2952 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

Why did you scrunch everything up like that?

You're missing the semicolon on the $file var assignment.

Copy and paste the code I posted. DO NOT retype it!


rudasi
Novice

May 20, 2010, 9:21 AM

Post #11 of 17 (2950 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post

Thanks a lot it seems to be working fine, could you pls explain the script to me cause I might need to make some changes later along the project?

Thanks


rudasi
Novice

May 20, 2010, 10:18 AM

Post #12 of 17 (2932 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post

The code doesnt seem to take in to account if the file is less than 8000 chars to start with, do you know what change can be made to account for this?

Thanks.


FishMonger
Veteran / Moderator

May 20, 2010, 10:48 AM

Post #13 of 17 (2928 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

It will handle a file of any size, even a file with only 1 character.

What changes did you make to the code I posted?

What parts do you not understand?


rudasi
Novice

May 20, 2010, 10:55 AM

Post #14 of 17 (2926 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post

Im a bit confused with the while loop.

What I understand of it is that the while loop first takes the first line in the $filehandle. Then removes the trailing new line.

Then checks to see if the the array files at its first index is greater than 8000 in length.

If its not (which it isnt initially) add the file to the index in the array.

As soon as the lenght of the array at that index is greater than 8000 increament the index and add the next file name to this index of the array.

Continue like this until you reach teh end of the file or there is no more input.

Is this correct?


FishMonger
Veteran / Moderator

May 20, 2010, 11:00 AM

Post #15 of 17 (2925 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

That is correct.


rudasi
Novice

May 20, 2010, 11:12 AM

Post #16 of 17 (2919 views)
Re: [FishMonger] String manipulation [In reply to] Can't Post

Thanks a lot for your hep, do you knw any beginners tutorials or online lesson for perl?


FishMonger
Veteran / Moderator

May 20, 2010, 11:19 AM

Post #17 of 17 (2916 views)
Re: [rudasi] String manipulation [In reply to] Can't Post

I'm old school...I use books.

Learning Perl, Fifth Edition
http://oreilly.com/catalog/9780596520113

Mastering Perl
http://oreilly.com/catalog/9780596527242/

Perl Best Practices
http://oreilly.com/catalog/9780596001735/

Additional Choices
http://oreilly.com/pub/topic/perl

 
 


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

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