Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Pls help: Appending/Joining words

meshtebmeshteb Posts: 47Member
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!

Comments

  • JonathanJonathan Posts: 2,914Member
    : $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
    on the end of $word. So you should do:-

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

    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
    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.");

  • meshtebmeshteb Posts: 47Member
    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
    on the end of $word. So you should do:-
    :
    : chomp $word;
    : $info = "$word $w1
    ";
    :
    : 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
    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.");
    :
    :

  • JonathanJonathan Posts: 2,914Member
    : 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.");

Sign In or Register to comment.