Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Pls help: Appending/Joining words Posted by meshteb on 9 Dec 2003 at 5:38 AM
Hi there

I was trying to join words here with this short program i wrote but it does not do what i expect.I want it to append the word "sp" on each input line form a file and put tha results on the other file.
E.g
If the input file is like this:

BEDS b eh d z
BEEN b iy n

Then the output file should look like this:

BEDS b eh d z sp
BEEN b iy n d s sp

Here is the program I wrote:

#!/usr/local/bin/perl -w

my $words="C:/WINNT/Profiles/Administrator/Desktop/input.txt";

my $out="C:/output.txt";

$w1= "sp";

# open prompt file
open my $fh, "< $words" || die ("Unable to open prompt file $prompt");

open my $wfh, "> $out" || die ("Can't create output file!");

while ( $word = <$fh>)
{
$info=join $word,' ',$w1;

print $wfh "$info";
}

close $fh;
close $wfh;

Thanx in advance!

Report
Re: Pls help: Appending/Joining words Posted by Jonathan on 9 Dec 2003 at 1:08 PM
: $info=join $word,' ',$w1;
This is not what join is for. Well, I guess it is in a way, but not like that. What you want is the concatenation operator:-

$info = $word . ' ' . $w1;

Or simpler, just interpolate:-

$info = "$word $w1";

Unfortunately that still won't work because there is a \n on the end of $word. So you should do:-

chomp $word;
$info = "$word $w1\n";

Which should work out fine.

--

Join takes an array as it's second parameter and inserts what is in the first parameter between them. So to use Join for what you wanted, you'd probably have needed to do:-

$info = join(' ', ($word, $w1));

The other methods I showed you read much easier than that, and you still need the chomp either way and to throw the \n back on the end of the new string. So join ain't so helpful here.

Hope this helps,

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: Pls help: Appending/Joining words Posted by meshteb on 12 Dec 2003 at 6:07 AM
Thanx for the help,it really did work. So how about to delete the phones from a file.

If my input file was like this:

EARLY er l iy sp
EARTHENWARE eh dh n w ea r sp

So that my output file can be like this:

EARLY
EARTHENWARE

Any clarity?






: : $info=join $word,' ',$w1;
: This is not what join is for. Well, I guess it is in a way, but not like that. What you want is the concatenation operator:-
:
: $info = $word . ' ' . $w1;
:
: Or simpler, just interpolate:-
:
: $info = "$word $w1";
:
: Unfortunately that still won't work because there is a \n on the end of $word. So you should do:-
:
: chomp $word;
: $info = "$word $w1\n";
:
: Which should work out fine.
:
: --
:
: Join takes an array as it's second parameter and inserts what is in the first parameter between them. So to use Join for what you wanted, you'd probably have needed to do:-
:
: $info = join(' ', ($word, $w1));
:
: The other methods I showed you read much easier than that, and you still need the chomp either way and to throw the \n back on the end of the new string. So join ain't so helpful here.
:
: Hope this helps,
:
: Jonathan
:
: ###
: for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
:

Report
Re: Pls help: Appending/Joining words Posted by Jonathan on 12 Dec 2003 at 7:23 AM
: Thanx for the help,it really did work. So how about to delete the
: phones from a file.
:
: If my input file was like this:
:
: EARLY er l iy sp
: EARTHENWARE eh dh n w ea r sp
:
: So that my output file can be like this:
:
: EARLY
: EARTHENWARE
:
: Any clarity?
So you just want the first word on the line, right? Provided the word is always at the start of the line, and you have the lines being put into $word, you can simply do:-

$word =~ s/^(\w+).*/$1/;

E.G. capture the first word (\w+), throw away the rest of the string .*, and then just put the first capture, e.g. $1. Note the newline character should be preserved as .* won't capture line breaks without a modifier to make it do so.

You can then print $word to the output file.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.