Find the answer to your Linux question:
Results 1 to 7 of 7
I'm looking for help with the print function. I have a script that will extract data from a web page, then pass that to Code: $data . I then send ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Location
    North Yorkshire, UK
    Posts
    31

    Perl - Print function


    I'm looking for help with the print function.

    I have a script that will extract data from a web page, then pass that to
    Code:
    $data
    . I then send this to a file using

    Code:
    open(FILE, ">>/path/to/file.txt") || die("Cannot open file");
    print FILE "$data";
    close(FILE);
    Then it moves to the next web page, and does the same...

    But it prints the next lot of data to the end of the text file, and I need it at the beginning. Is there a way to do this?

    I'm trying to avoid having to set each lot of $data to an array, and then reversing the array (I think that's possible?), since there will be about 4.5 MB of data in total.

    Thanks in advance for any help!

  2. #2
    Just Joined!
    Join Date
    Sep 2006
    Location
    Cincinnati, OH, USA
    Posts
    6
    Your question really doesn't involve the print function, but more what is in $data. Print does what it's told, display what is in $data. Print is a very useful debug tool. You must be setting $data to the wrong thing.

    A little more info on $data would be helpful.

  3. #3
    Just Joined!
    Join Date
    Mar 2007
    Location
    North Yorkshire, UK
    Posts
    31
    well... $data is anything... it's just a string.

    As I understand it, $data is a variable, and whether it equals "", or a whole novel, it treats it as one long string (including new lines, etc.).

    In this case, $data = "some html here";

    It's a single line of HTML code.

    I open the file
    Code:
    open(FILE, "/path/to/file.txt");
    Then I add the HTML to the file
    Code:
    print FILE "$data\n";
    I want to be able to add the HTML to the beginning of the file, though.

    As an example, if it makes things simpler...

    $data = "<a href=\"mypage.com\/page01.htm\">Page 1</a><br>\n";

    If I set up a script to make an index of the pages on a page, then I'd set up a for loop, to create the HTML for each link. Then I'd add each line of HTML to a separate file, to create a web page containing links to each page.
    But if I open the file and use print to add the HTML to the file, then it adds to the end. So if I were to input the number of pages, and set the loop to continue until it reached page 1, then it would go backwards.
    However, the last line of HTML would be for page 1, and if I use "print", then it'll add it to the end.

    I know that I could do it like that, then re-open the file, assign the contents to an array, reverse the order of the array and then print to file again.
    But it's an extra step I'm trying to get around, and with there being 50,000 items in the array, it'll be a bit of a strain, I think, so I want to avoid doing that.

    And, of course, I can always run through the loop forwards (which is what I'm using now), to get them in the right order. But for future reference, I was wondering if there was some other quick way of doing it?

    And I thought of adding each $data string to a second variable, building it up in the right order, but that's similar to using an array, I think...

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru bryansmith's Avatar
    Join Date
    Nov 2004
    Location
    /Ontario/Canada
    Posts
    2,619
    Why not create a second string, add the new data to that and then make $data equal to the second string + $data.

    So (I don't know much perl so bear with me), your code would look like this:
    Code:
    $data = "the html";
    $the_file = the contents of the file
    $the_file = $data + $the_file;
    I don't much perl so I don't know if that will work (or even if that is what you are looking for).
    Looking for a distro? Look here.
    "There can be no doubt that all our knowledge begins with experience." - Immanuel Kant (Critique of Pure Reason)
    Queen's University - Arts and Science 2008 (Sociology)
    Registered Linux User #386147.

  6. #5
    Just Joined!
    Join Date
    Mar 2007
    Location
    North Yorkshire, UK
    Posts
    31
    Yeah, that would work, Bryan, and I'd thought of that.

    In this case, I've solved it. And I'd end up with 4.5MB of data in the $the_file variable there, which I was trying to get around.

    But I'm still wondering if there's a way of opening a file, using "print" to add to the beginning, and then close the file. Or if the "print" command can ONLY add to the end.
    If that's the case, then I can get around that. But it'd be nice to know, one way or the other.

    Though, from what I've found, it doesn't look like you can...

  7. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,303
    Hi.

    You may know about appending data to a file, and it sounds like you would like to have a prepend file operation. I have not seen that in *nix.

    I think I would write the new data to a file, then use cat to combine the files, and rename the new file. Using perl to read and write a big file line-by-line is going to be be expensive compared to block reads and writes (for the usual case of cat).

    If the pages are not used really often, you could store everything backwards, append new data backwards (with perl or tac), and serve the page also with tac.

    I briefly thought about suggesting server-side includes, but that appears to be resource-intensive ... cheers, drl

    ( edit 1: revise )
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #7
    Just Joined!
    Join Date
    Mar 2007
    Location
    North Yorkshire, UK
    Posts
    31
    Thanks, drl.

    I guess I do want a prepend operation... it's a shame it doesn't exist.
    But knowing that, I can find ways to get around it, so that's not a problem.

Posting Permissions

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