Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm going to offer a two part answer. I'll first answer the question you asked, then I'll answer what it looks like you're really trying to do with your code :-)

1. The question you asked -- how to read data from a newline -- I think you may be making this problem harder then it is. Perl will gladly work with you on this. In fact, by default, it does read line by line, unless you tell it differently. As an example, check out this code:
# You are using strict, right??? :-) use strict; my $adfile = 'banners.dat'; open(FILE, $adfile) || die "Can't open $adfile"; # Loops through the data file, 1 line at a time while(<FILE>) { # Split apart the line of data, put it in an array my @part = split(/\|/, $_); # Print's the url from the data line print "$part[0]\n"; } # Close up the file close(FILE);
That code will loop through your data file, line by line. By the time it's finished executing, it will print the url from each line of your data file.

However, it doesn't appear that you need to do something with each line, it appears that you want to pick out a random line from your data file, and do something with it. And you're in luck, because it just so happens that this information is in the FAQ. All you have to do to get a random line from your file is this:
srand; # This chooses a random line, and puts that line into # the var $line. rand($.) < 1 && ($line = $_) while <FILE>; # Now you can split it my @part = split(/\|/,$line); # And then do whatever else with it... print "$part[0] $part[1] $part[2] ... ";
As a final note, I noticed that you were accessing elements in the @part array by using syntax like @array[0]. Actually, the correct syntax to access a single element in an array is $array[0] and $array[1]. It's using the $, instead of the @. The @ is for referring to an entire array or array slice. The $ is used to refer to one, scalar value, such as a single element in an array.

Hope that helps!
-Eric

In reply to Re: telling script to read from next line by andreychek
in thread telling script to read from next line by ginocapelli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2024-04-19 00:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found