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

Hi all, I hope every monk had a great christmas :)

Well, I posted here about 5 days ago about how to get started with Perl. First of all I would like to thank the people who answered my post:

Joost
davido
punkish
wolfger
mkirank
BravoTwoZero
melora
shodckwm
tphyahoo
Plotinus
robot_tourist

Your help was invaluable, thanks you so much, you guys are great. :)

Now, I currently have a very small newbie question that I can't seem to find the answer anywhere. Itīs about strings. I was just wondering how can I remove all the "/n" from a text array that was red from a file. Here's the code so you can get the idea:
my $file = './tcard.conf'; open(INFO, $file); my @lines = <INFO>; close(INFO); my $url = $lines[0];
Well, the tcard.conf file has a list of URLs. The problem is that the string i get from $lines[0] is not a valid URL. My guess is that there is a "/n" at the end of each line, and that is why it won't allow me to get it. So, I would like to know a way to remove all the "/n" from the array.

Thanks!!

Replies are listed 'Best First'.
Re: How to eliminate "/n" from text
by osunderdog (Deacon) on Dec 29, 2004 at 23:39 UTC

    chomp


    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


    OSUnderdog
      oh, that was simple :s
      i feel stupid :p
      thanks a lot osunderdog
Re: How to eliminate "/n" from text
by Old_Gray_Bear (Bishop) on Dec 29, 2004 at 23:58 UTC
    As osunderdog noted, chomp is the way to go, it your guess is correct. However, it might be a bit more useful to know rather than guess.
    my $file = './tcard.conf'; open(INFO, $file); my @lines = <INFO>; close(INFO); print("The first line is >>$lines[0]<<\n"); my $url = $lines[0];
    By surrounding the input line with markers (the '>>' and '<<'), you will be able to see if there is a final \n causing you a problem. It may be that the first line of the file is blank, you know.

    If you haven't gotten a copy yet, go buy an Alpaca (Learning Perl by Schwartz (aka merlyn) and Phoenix). They develop a file reading code as an introduction to Perl; the section is titled "The chomp Operator", in chapter two.

    ----
    I Go Back to Sleep, Now.

    OGB

      I see.
      The final \n was the problem alright, but I'll keep the ('>>' and '<<') in mind from now on.
      And I'll start looking for that Alpaca soon.
      Thanks a lot for the info :)
Re: How to eliminate "/n" from text
by larryp (Deacon) on Dec 30, 2004 at 04:22 UTC

    Hi Deib,

    One thing I want to point out: you are currently reading the entire file into an array. Is this necessary? Would a loop suffice? From the example you provide, it seems like you're only using the first entry in the file. Is this true?

    It might be more memory efficient to loop over the contents of your file. To do so, replace everything between the open and close statements with this code:

    # The while loop reads each line of the file, one at a time. You can # then process each line individually. while( <INFO> ) { chomp( $_ ); # This removes the newline from each line. # Add code to process each URL. }

    If the file is small, it probably doesn't make much difference. If the file is large, looping over the file will be nicer to your machine.

    If you really only need the first line of the file, use this code:

    open( INFO, $file ); # This will only read the file up to the first newline. If the lines # in the file are delimited with newlines, it will read the first # line in the file and stop. my $line = <INFO>; close( INFO ); chomp ( $line ); # Add code to process the first URL.

    It might seem "nit-picky", but I think it's a good habit to develop early. :)

    If you really do need to read the entire file into an array, add this line to your script, after you load the array:

    # Removes newlines from each array element at once. chomp( @lines );

    HTH,

    /Larry

      Moreover, you can do this:

      open(foo,"bar") or die;
      chomp(my @baz = <FOO>);
      close(foo) or die;

      While intended to show another way to do chomp, the neat
      thing is that you can open a file, save the contents to
      an array, and then close the file.

      Thanks to various people on perlmonks for helping me
      learn that.
        Thanks for that nice and simple way of doing it, Anonymous. :)
        I appreciate it. ^_^
      (In this post, when I say "file" I mean the .inf file, not the perl file)

      Larry, thanks a lot for posting.
      Sorry I'm late with the reply, we had some issues with my workspace, so I couldn't work on this for a few days.

      There are many URLs in the file, in the example code I only take the first line because I was just doing a test. Sorry that I did not specify.

      Actually, the file is constituted of 2 URLs per process. So I divide the lines in 2 different arrays. One with the even lines and one with the odd ones. Let's suppose that the file I use is like this:

      www.google.com/folder/ www.google.com/folder/onething.htm www.hotmail.com/folder/ www.hotmail.com/folder/anotherthing.htm


      So, for each process I have to use the URLs in pairs.
      I use a for to do it. However, that while( <INFO> ) way you told is great!! :)

      The only way I know how to do what i want is with a for and a counter to separate the odds and evens.

      Any ideas on alternatives that can be more efficient?? It can involve the code or the way the file is arranged.
      I feel really narrow-minded right now since I'm a newbie, so any input is greatly appreciated. I hope my post is clear enough. Thanks!! :)

        Hi Deib,

        Sorry for the delay in my response. I've been out of town since Jan. 5 and I just got back yesterday.

        Are you creating the data file you're using? If so, it seems like you're creating extra work for yourself. It seems like the first line is the root of the site (or site folder) and the second line is the path to the page. Why don't you simply load the file with the lines that represent the paths to the pages and use these to parse out the site paths?

        You could use the File::Basename module to parse the page paths. Consider this:

        #!/usr/bin/perl use strict; use File::Basename; my @paths = ( 'www.google.com/folder/', 'www.google.com/folder/onething.htm', 'www.hotmail.com/folder/', 'www.hotmail.com/folder/anotherthing.htm', 'www.hotmail.com/folder' ); foreach my $path ( @paths ) { print "PATH: $path\n"; # This line does all the work. my ($base, $dir, $ext) = fileparse( $path, '\.[^.]*' ); printf "DIR: $dir\nBASE: $base\nEXT: $ext\n\n"; }

        The commented line in the foreach loop is doing all the parsing work. It splits the path you give it into three pieces: The directory path to the file, the basename (the part of the file name before the extension), and the extension (the part of the file name after the final period.) Of course, if the file name does not contain an extension, the entire part after the final slash is returned as the basename.

        This is certainly a more efficient way to process the input file since it reduces the number of lines you have to process by 50%. Of course, if you have to use the file the way it stands in the example you provided, this might not help as much. :)

        Without knowing exactly what it is you're trying to do, I'm afraid I'll spend a lot of time guessing what your requirements are.

        Let me know if you have other questions or if this is unclear.

        HTH,

        /Larry

Re: How to eliminate "/n" from text
by geektron (Curate) on Dec 29, 2004 at 23:50 UTC
    perldoc -f chomp

    chomp removes trailing newline characters.

Re: How to eliminate "/n" from text
by BUU (Prior) on Dec 30, 2004 at 00:44 UTC
    Since nobody else has pointed this out, I feel compelled to complain: /n is two characters. A slash and a N. It is not the escape for a new line, at least in any language I'm familiar with. You probably want \n, which *is* a new line in Perl, C, C++, and others.