[prog] perl string question

Jacinta Richardson jarich at perltraining.com.au
Fri Feb 28 12:41:39 EST 2003


On 27 Feb 2003, mc wrote:

> ^~00002439:0000007179:000016:Message-ID: <3E33C3BD.21CD8BB6 at att.net>
> 
> I would like to output this to my working file so it ends up as:
> 
> ^~00002439:0000007179:000016:
> Message-ID: <3E33C3BD.21CD8BB6 at att.net>
> 
> I thought I could do this with the split function, but since I only want
> to split it at the third ":" I am not sure how to write that.  I am also
> lost as to how then to print the results to my file.  I am sure it is a
> print statement, just a bit lost :)

You can use split:

my ($part1, $part2, $part3, $messageid) = split ':', $line;
$part1 = join '', $part1, $part2, $part3;

or

my @bits = split ':', $line;
my $messageid = pop @bits;
my $part1 = join '', @bits;

or

my ($part1, $messageid) = ($line =~ m/^(.+):(Message.+)$/);


As to how to print to a file.

open FILE, "< $file" or die "Failed to open $file for reading: $!";
print FILE "$part1\n$messageid\n";
close FILE;

Good luck with it.

	Jacinta


--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |





More information about the Programming mailing list