Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have a text file which contains 4 names printed line by line. I want to read the names line by line and modify the names in a form which is separated with '|'. For a more clear info, my input will look like this, Matei Janov rebas yoni and i want out in a 'print' variable as 'Matei|Janov|rebas|yoni' Pls help Monks. I am not a matured Perl programmer! Thx in adv.
  • Comment on Read Text line by line & modify with a delimiter

Replies are listed 'Best First'.
Re: Read Text line by line & modify with a delimiter
by LanX (Saint) on Dec 20, 2012 at 21:14 UTC
    please use <code>tags for your examples

    I think you want(?) this:

    open my $fh,"<","textfile"; my @lines = <$fh>; close $fh; print join "|", @lines;

    untested!

    Cheers Rolf

      Thx, but what can we do if the name contains a leading space or trailing space and I want to delete those spaces?

      Thx for your help.

      thx for doing my homework! :)
Re: Read Text line by line & modify with a delimiter
by Anonymous Monk on Dec 20, 2012 at 21:28 UTC
Re: Read Text line by line & modify with a delimiter
by 2teez (Vicar) on Dec 20, 2012 at 22:07 UTC

    Or this:

    chomp( my @names = <DATA> ); $" = "|"; print "@names"; __DATA__ Matei Janov rebas yoni
    Output:
    Matei|Janov|rebas|yoni

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Use something like:
      print join ("|", $name1, $name2);

      Thx, but how can i delete a leading spaces or trailing space from the names, Is it possible?

      Thx for your help.

        Have a look at regexes
Re: Read Text line by line & modify with a delimiter
by RFMonk (Acolyte) on Dec 20, 2012 at 23:58 UTC
    maybe want to use
    print join ("|", $names1, $names2);

    cheers (/p>

      You better understand the question and write an answer. Don't waste your time in posting waste.... :D