Professional Web Applications Themes

Avoiding using temporary files - PERL Beginners

Dear all, First post to this list! Hope I do it right! I'm looking for a way to print to a temporary filehandle or something without having to open a file to print to. I am running some substitutions on a file which has a distinctive record structure. Each record is printed to the output filehandle in turn after the substitutions have been performed. Once all records are printed out I need to go back over them i.e. go back to the top and do some further work on the OUT filehandle. I am currently using SEEK and while (<OUT>) ...

  1. #1

    Default Avoiding using temporary files

    Dear all,

    First post to this list! Hope I do it right!

    I'm looking for a way to print to a temporary filehandle or something
    without having to open a file to print to.

    I am running some substitutions on a file which has a distinctive record
    structure. Each record is printed to the output filehandle in turn after
    the substitutions have been performed. Once all records are printed out
    I need to go back over them i.e. go back to the top and do some further
    work on the OUT filehandle. I am currently using SEEK and while (<OUT>)
    and a temporary file connected to OUT to achieve this. Is there a way to
    go back over the output without using a temporary file?

    $/ = "</rec>"
    while (<CUTLINK>)

    {
    s/\r//g;

    $last_id = $current_id;
    $last_level = $current_level;

    if (/<id>(.*)<\/id>\n<level>([0-9]+)<\/level>/)

    {
    $current_id = $1;
    $current_level = $2;
    $splice_position = $2 - 1;
    $parent_position =$2 - 2;
    #print STDOUT
    "$current_id\t$current_level\t$splice_position \n";
    splice (parents, $splice_position, 1,
    $current_id);
    #print $parents[$splice_position];

    s/<body>/<parent>$parents[$parent_position]<\/parent>\n<body>/;
    #print $parents[$parent_position];
    }

    print OUT;
    }




    Richard
    Richard Guest

  2. #2

    Default Re: Avoiding using temporary files

    From: "Barrett-Small, Richard" <co.uk> 

    You can use IO::String or IO::stringy to create a filehandle that
    points to a string, not to a file. (I believe IO::String is part of
    the core in recent perls, IO::stringy may be installed from CPAN or
    PPM)
     

    If you can change the code it might be best not to print them at all.

    You may either push them into an array or append them into a string.

    Jenda
    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

  3. #3

    Default Re: Avoiding using temporary files

    --As of Tuesday, April 13, 2004 2:16 PM +0200, Jenda Krynicky is alleged to
    have said:
     
    >
    > If you can change the code it might be best not to print them at all.[/ref]

    --As for the rest, it is mine.

    Another option, if the above has problems for you, is to print them to a
    variable: Perl 5.8 can open a scalar as a filehandle. Do your writing, the
    close the filehandle and use the scalar directly.

    Not printing at all would be best though, if the code can afford it.

    That is, if can afford to hold the whole file in memory... (Which both of
    these do.)

    Daniel T. Staal

    ---------------------------------------------------------------
    This email copyright the author. Unless otherwise noted, you
    are expressly allowed to retransmit, quote, or otherwise use
    the contents for non-commercial purposes. This copyright will
    expire 5 years after the author's death, or in 30 years,
    whichever is longer, unless such a period is in excess of
    local copyright law.
    ---------------------------------------------------------------

    Daniel Guest

  4. #4

    Default Re: Avoiding using temporary files

    Thank you so much for this, Jenda and Daniel>

    Could you give me a stab of code demonstrating appending or printing to a
    scalar and also how I might avoid printing OUT but retain the changes I
    made to the filehandle so they can be passed to another loop. Memory
    shouldn't be an issue

    Richard
    ..
    "Daniel Staal" <net> wrote in message
    news:2147483647.1081855924[192.168.1.50]... 
    to 
    > >
    > > If you can change the code it might be best not to print them at all.[/ref]
    >
    > --As for the rest, it is mine.
    >
    > Another option, if the above has problems for you, is to print them to a
    > variable: Perl 5.8 can open a scalar as a filehandle. Do your writing,[/ref]
    the 


    Richard Guest

  5. #5

    Default Re: Avoiding using temporary files

    Richard,

    I wouldn't look to use a temporary file. In stead push your output into
    an array then iterate through the array on your second pass. I would
    expect you to get better performance as you won't have to worry about
    disk I/O.

    Regards,
    Adam

    On Apr 13, 2004, at 4:55 AM, Barrett-Small, Richard wrote:
     

    Adam Guest

  6. #6

    Default RE: Avoiding using temporary files

    Thank you so much for this, Jenda and Daniel and Adam

    Could you give me a stab of code demonstrating appending or printing a
    record (like the one below) to a
    scalar (or pushing to an array) and how I might avoid printing OUT but
    retain the changes I
    made to the filehandle so they can be passed to another loop. Memory
    shouldn't be an issue.

    Regards,

    Richard

    -----Original Message-----
    From: Daniel Staal [mailto:net]
    Sent: 13 April 2004 16:32
    To: Perl Beginners
    Subject: Re: Avoiding using temporary files

    --As of Tuesday, April 13, 2004 2:16 PM +0200, Jenda Krynicky is alleged
    to have said:
     
    >
    > If you can change the code it might be best not to print them at all.[/ref]

    --As for the rest, it is mine.

    Another option, if the above has problems for you, is to print them to a
    variable: Perl 5.8 can open a scalar as a filehandle. Do your writing,
    the close the filehandle and use the scalar directly.

    Not printing at all would be best though, if the code can afford it.

    That is, if can afford to hold the whole file in memory... (Which both
    of these do.)

    Daniel T. Staal

    ---------------------------------------------------------------
    This email copyright the author. Unless otherwise noted, you are
    expressly allowed to retransmit, quote, or otherwise use the contents
    for non-commercial purposes. This copyright will expire 5 years after
    the author's death, or in 30 years, whichever is longer, unless such a
    period is in excess of local copyright law.
    ---------------------------------------------------------------



    Richard Guest

Similar Threads

  1. [Acrobat 7 Pro] Avoiding log files with PDF/X
    By Thomas G. Madsen in forum Adobe Acrobat Macintosh
    Replies: 2
    Last Post: December 1st, 07:08 PM
  2. Help! - ASP.NET cannot 'Temporary ASP NET Files'
    By Martin Erskine in forum ASP.NET Security
    Replies: 0
    Last Post: September 17th, 07:19 PM
  3. Where is Temporary Files for ASP.NET?
    By poi in forum ASP.NET General
    Replies: 1
    Last Post: August 11th, 04:29 PM
  4. Temporary files
    By John in forum Windows XP/2000/ME
    Replies: 2
    Last Post: July 28th, 06:12 PM
  5. Temporary files
    By Tight Red Skin in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 2
    Last Post: July 2nd, 10:47 AM

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