Professional Web Applications Themes

foreach (from a file) - PERL Beginners

Hi All, If I have a file, /usr/bin/my_urls.txt which contain... urls... :-) one on each line. Can I read these into a foreach statement instead of listing them individually? I think I need to use a filehandle to open the file and then send that to the ... right? ------ maybe like this? ---------- urls = /usr/bin/my_urls; foreach $urls (urls) { print ------- end -------------------------- --- here is what I'm doing now ------- use strict; use LWP::Simple; foreach my $url ( 'http://www.bvolk.com', 'http://www.tvolk.com', 'http://www.', ) { print "\n$url\n"; my ($type) = head($url); unless (defined $type) { print "Couldn't get $url\n"; ...

  1. #1

    Default foreach (from a file)

    Hi All,

    If I have a file, /usr/bin/my_urls.txt which contain... urls... :-) one on
    each line. Can I read these into a foreach statement instead of listing
    them individually? I think I need to use a filehandle to open the file and
    then send that to the ... right?

    ------ maybe like this? ----------

    urls = /usr/bin/my_urls;
    foreach $urls (urls) {
    print
    ------- end --------------------------


    --- here is what I'm doing now -------

    use strict;
    use LWP::Simple;
    foreach my $url (
    'http://www.bvolk.com',
    'http://www.tvolk.com',
    'http://www.',
    )
    {
    print "\n$url\n";

    my ($type) = head($url);

    unless (defined $type) {
    print "Couldn't get $url\n";
    next;
    }
    }
    --------- end ----------------------------

    need help...!

    Thanks!

    Brian
    Brian Guest

  2. #2

    Default Re: foreach (from a file)

    On Jul 15, 2004, at 9:40 AM, Brian Volk wrote:
     

    Exactly, right. I'll give the steps:

    1. open() your file. Remember to check if it succeeds!

    2. Read line by line, chomp()ing line endings and adding them to an
    array.

    3. close() fiie and use array in foreach loop.

    Just shout if you need more help...

    James

    James Guest

  3. #3

    Default RE: foreach (from a file)

    Am I getting close...?

    my $file = "/Program Files/OptiPerl/urls.txt";
    open (LINKS, $file) or die "Can't open $file: $!";

    chomp (url = read(LINKS, $url, 100));

    Thanks for your help!

    Brian


    -----Original Message-----
    From: James Edward Gray II [mailto:net]
    Sent: Thursday, July 15, 2004 10:10 AM
    To: Brian Volk
    Cc: Beginners (E-mail)
    Subject: Re: foreach (from a file)


    On Jul 15, 2004, at 9:40 AM, Brian Volk wrote:
     

    Exactly, right. I'll give the steps:

    1. open() your file. Remember to check if it succeeds!

    2. Read line by line, chomp()ing line endings and adding them to an
    array.

    3. close() fiie and use array in foreach loop.

    Just shout if you need more help...

    James


    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Brian Guest

  4. #4

    Default Re: foreach (from a file)

    On Jul 15, 2004, at 10:33 AM, Brian Volk wrote:
     

    Sure are.
     

    We're fine up to here.
     

    Let's break that into two steps:

    my urls = <LINKS>; # slurp the file

    chomp urls; # remove line endings
     

    My pleasure.

    James

    James Guest

  5. #5

    Default RE: foreach (from a file)

    James,

    Thanks so much for your help, it's now working great!

    One question, if you don't mind...
    my urls = <LINKS>; # slurp the file
    Is this telling the diamond operator what to use for input?
    Just want to make sure I'm understanding correctly.

    Thanks again,

    Brian

    -----Original Message-----
    From: James Edward Gray II [mailto:net]
    Sent: Thursday, July 15, 2004 10:38 AM
    To: Brian Volk
    Cc: 'Beginners (E-mail)'
    Subject: Re: foreach (from a file)


    On Jul 15, 2004, at 10:33 AM, Brian Volk wrote:
     

    Sure are.
     

    We're fine up to here.
     

    Let's break that into two steps:

    my urls = <LINKS>; # slurp the file

    chomp urls; # remove line endings
     

    My pleasure.

    James


    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Brian Guest

  6. #6

    Default Re: foreach (from a file)

    On Jul 15, 2004, at 10:56 AM, Brian Volk wrote:
     

    Happy to help.
     

    Sure.
     

    You got it. Because we're assigning to an array it's called in "list
    context". The diamond operator, in list context, returns the rest of
    the lines of the file. This is generally called "slurping".

    Hope that helps.

    James

    James Guest

Similar Threads

  1. Bug in Foreach.....???
    By Rob in forum PHP Development
    Replies: 2
    Last Post: December 23rd, 05:20 PM
  2. Using while and foreach
    By Marco in forum PHP Development
    Replies: 4
    Last Post: February 4th, 12:12 PM
  3. foreach
    By Eric Walker in forum PERL Beginners
    Replies: 12
    Last Post: December 19th, 03:47 PM
  4. CGI.pm and foreach
    By shade in forum PERL Miscellaneous
    Replies: 8
    Last Post: August 7th, 10:10 AM
  5. if-map vrs foreach-if
    By fatted in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 15th, 02:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139