[23448] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 5663 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 15 14:05:56 2003

Date: Wed, 15 Oct 2003 11:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 15 Oct 2003     Volume: 10 Number: 5663

Today's topics:
    Re: add text to next line? (Anno Siegel)
    Re: add text to next line? (Tad McClellan)
    Re: add text to next line? <geoff.cox@blueyonder.co.uk>
    Re: add text to next line? <peter@semantico.com>
    Re: add text to next line? (Roy Johnson)
    Re: add text to next line? (Roy Johnson)
        ANNOUNCE: Java::Build an Ant replacement (Phil Crow)
    Re: check if a string ends with particular letters <REMOVEsdnCAPS@comcast.net>
    Re: cleanup speed (named vs anonymous (and my?)) nobull@mail.com
        Getting part of a number <slack3r@nowhere.com>
    Re: Getting part of a number <HelgiBriem_1@hotmail.com>
    Re: Getting part of a number <some@one.com>
    Re: Getting part of a number <slack3r@nowhere.com>
        Getting values from a CGI call <nick@maproom.co.uk>
    Re: Getting values from a CGI call <pinyaj@rpi.edu>
    Re: Getting values from a CGI call <nick@maproom.co.uk>
        How can i write the values of a form through a cgi scri (JR)
    Re: How does one move down a line in a file? <xx087@freenet.carleton.ca>
    Re: How does one move down a line in a file? (Roy Johnson)
    Re: How does one move down a line in a file? <jwillmore@remove.adelphia.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 15 Oct 2003 13:05:24 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: add text to next line?
Message-Id: <bmjgmk$a86$1@mamenchi.zrz.TU-Berlin.DE>

Geoff Cox  <geoff.cox@blueyonder.co.uk> wrote in comp.lang.perl.misc:
> Hello,
> 
> I am trying to find <body> in an html file and then add some text to
> the line after the <body> tag ... the code below adds the text at the
> end of the file.   How do I get the text on the next line after
> <body>?
> 
> Cheers
> 
> geoff
> 
> 
>     use warnings;
>     use strict;
> 
>     use File::Find;
>     
>     my $dir = 'd:/a-winfiles/mfox/html/few';
> 
>     
>     find sub {
>     my $name = $_;
>     if (-d $_) {return;};
>     if ($name !~ /^print/) {
>     open (IN, "$name");
>     my $line;
>     while (defined ($line = <IN>)) {
>     if ($line  =~ /<body>/i) {
>     open (OUT,">>$name");
>     print OUT ("\n freddy");
>     }
>     }
>     }
>         
>         }, $dir;

Parsing HTML this way will be fragile.  Not every occurrence of "<body>"
must be an HTML tag.

To do this, you will basically copy the input file to the output.
While doing this, watch each line for <body> and keep the result
for the next round.  If this indicates that the last line contained
"<body>", change the current line before printing it.  This is
the loop (untested):

    my $seen_body;
    while ( <IN> ) {
        if ( $seen_body ) {
            # make the necessary changes to $_
        }
        print OUT $_;
        $seen_body = /<body>/;
    }

This assumes that the changes don't change the absence or presence
of "<body>" in the modified line.

Anno


------------------------------

Date: Wed, 15 Oct 2003 09:02:11 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: add text to next line?
Message-Id: <slrnboqkr3.7cn.tadmc@magna.augustmail.com>

Geoff Cox <geoff.cox@blueyonder.co.uk> wrote:

> I am trying to find <body> in an html file 


Then it might look like this you know:

   <body    >
or
   <body
   >


What will your code do if this occurs before the real body start tag?

   <!-- add stuff after the <body> tag.  -->


You should use a module that understands HTML for processing HTML data.


> and then add some text to
> the line after the <body> tag ... the code below adds the text at the
> end of the file.   


Because you are open()ing the file for appending.


> How do I get the text on the next line after
><body>?


   perldoc -q line

       How do I change one line in a file/delete a line in a
       file/insert a line in the middle of a file/append to the
       beginning of a file?


You are expected to check the Perl FAQ *before* posting to the
Perl newsgroup.


>     if (-d $_) {return;};


   return if -d;  # does the same thing without obfuscating punctuation


>     open (IN, "$name");
                ^     ^
                ^     ^ What's wrong with always quoting "$vars"?


You should always, yes *always*, check the return value from open():

   open(IN, $name) or die "could not open '$name'  $!";


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Wed, 15 Oct 2003 15:00:32 GMT
From: Geoff Cox <geoff.cox@blueyonder.co.uk>
Subject: Re: add text to next line?
Message-Id: <n2nqovg6i1m2igf0kp81dr4l3oi68ug58q@4ax.com>

On Wed, 15 Oct 2003 09:02:11 -0500, tadmc@augustmail.com (Tad
McClellan) wrote:

>Geoff Cox <geoff.cox@blueyonder.co.uk> wrote:
>
>> I am trying to find <body> in an html file 
>
>
>Then it might look like this you know:
>
>   <body    >
>or
>   <body

Tad,

The reason I have asked about this is that I have 200 html files which
use large fonts for the text. For prinitng purposes I wish to have a
copy of each file but with small font text.. 

The idea is to have a link in each large font file to the smaller font
copy, which has the same name except it has print- at the beginning of
the name.

I can easily add (using find/replace function of html editor) a link
to each file with say "print-file"  in the link. I would then need a
Perl programme to replace file with the name of the file.

Any pointers please on simply being able to find a word and replace it
with another word?

Cheers

Geoff

>
>What will your code do if this occurs before the real body start tag?
>
>   <!-- add stuff after the <body> tag.  -->
>
>
>You should use a module that understands HTML for processing HTML data.
>
>
>> and then add some text to
>> the line after the <body> tag ... the code below adds the text at the
>> end of the file.   
>
>
>Because you are open()ing the file for appending.
>
>
>> How do I get the text on the next line after
>><body>?
>
>
>   perldoc -q line
>
>       How do I change one line in a file/delete a line in a
>       file/insert a line in the middle of a file/append to the
>       beginning of a file?
>
>
>You are expected to check the Perl FAQ *before* posting to the
>Perl newsgroup.
>
>
>>     if (-d $_) {return;};
>
>
>   return if -d;  # does the same thing without obfuscating punctuation
>
>
>>     open (IN, "$name");
>                ^     ^
>                ^     ^ What's wrong with always quoting "$vars"?
>
>
>You should always, yes *always*, check the return value from open():
>
>   open(IN, $name) or die "could not open '$name'  $!";



------------------------------

Date: Wed, 15 Oct 2003 16:10:09 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: add text to next line?
Message-Id: <3f8d6351$0$11455$afc38c87@news.easynet.co.uk>

Geoff Cox wrote:
> The reason I have asked about this is that I have 200 html files which
> use large fonts for the text. For prinitng purposes I wish to have a
> copy of each file but with small font text.. 
> 
> The idea is to have a link in each large font file to the smaller font
> copy, which has the same name except it has print- at the beginning of
> the name.
> 
> I can easily add (using find/replace function of html editor) a link
> to each file with say "print-file"  in the link. I would then need a
> Perl programme to replace file with the name of the file.
> 
> Any pointers please on simply being able to find a word and replace it
> with another word?
> 
> Cheers
> 
> Geoff

What you prehaps should be looking at is CSS, you can have one stylesheet for 
screen display and another for printing. The use just has to select that they 
wish to print the page and the printing stylesheet will be used.



------------------------------

Date: 15 Oct 2003 10:11:39 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: add text to next line?
Message-Id: <3ee08638.0310150911.37cb41cd@posting.google.com>

Geoff Cox <geoff.cox@blueyonder.co.uk> wrote in message news:<n2nqovg6i1m2igf0kp81dr4l3oi68ug58q@4ax.com>...

> Any pointers please on simply being able to find a word and replace it
> with another word?

Assuming only one body tag per file:

perl -pe 's/<body>/<new body>/' file.html > file-print.html


------------------------------

Date: 15 Oct 2003 10:15:35 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: add text to next line?
Message-Id: <3ee08638.0310150915.6c632145@posting.google.com>

Sorry, I answered the question you posted there, but not the question
you started this thread about. To add a line after each line
containing <body>:

while (<>) {
    print;
    print "Whatever new text\n" if /<body>/;
}


------------------------------

Date: Wed, 15 Oct 2003 16:11:15 GMT
From: philcrow2000@yahoo.com (Phil Crow)
Subject: ANNOUNCE: Java::Build an Ant replacement
Message-Id: <HMt527.19KE@zorch.sf-bay.org>

ANNOUNCING
    Java::Build version 0.03

ADVERTISEMENT
    Are you tired of trying to script in Ant build files? Would you like to
    control your build from perl, the language recognized around the world
    as the leader in text processing and operating system interaction? Do
    you wish your build scripts were compiled before being run? Do you long
    to control your Java builds from Perl? Wait no longer for relief,
    Java::Build is here.  CPAN servers are standing by, download it now.

RATIONALE
    Ant started because make invoked javac once for each file to be
    compiled. Since javac is a Java program, that caused build delays as a
    new Java Virtual Machine (JVM) started for each source code file. The
    rest of Ant's features are really immaterial compared to the javac
    problem. (Platform independence is the only other one I find
    compelling.)

    What we wanted was a way to start a single JVM for compiling, then
    control it with Perl. That's what Java::Build::JVM does. Many tasks
    which Ant does are already builtin functions in Perl. The others can
    easily be implemented. Java::Build::Tasks includes some high value
    helper functions we like. Feel free to write your own in whatever way
    you prefer. But, please consider sending them in, so other people can
    use them too.

SYNOPSIS
        use Java::Build::JVM;
        use Java::Build::Tasks;
        my $compiler = Java::Build::JVM->getCompiler();

        my $sources = build_file_list(
            BASE_DIR         => '/where/the/sources/are',
            INCLUDE_PATTERNS => [ qr/.java$/ ],
            EXCLUDE_PATTERNS => [ qr/Test$/, qr/examples/ ],
        );  # returns a list of source file names

        $compiler->compile(what_needs_compiling($sources));
        # use $sources from build_file_list, or make your own list
        # hint: $compiler->compile([ qw( list files here ) ]);

DISCUSSION
    By using Inline::Java and the same technique Ant itself uses to conserve
    JVM's for compiling, Java::Build provides the ability to control builds
    from Perl. This is a tremendous advantage for complex builds.

    My boss started me working on this code out of frustration with Ant
    features like

    *   festering errors (Ant does not verify the syntax of the build xml as
        a whole, validation is done only when the code is reached, so Ant is
        an interpretter of its special xml based not quite scripting
        language)

    *   properties which retain their values even if later statements seem
        to be changing them

    *   absence of loops and functions

    *   any additions must not only be coded in Java (not noted for its text
        processing and OS prowess), but they must be built into your Ant

    *   cumbersome conditional logic (remember, Ant builds are described by
        xml)

    *   No CPAN

    If you like Ant, you probably won't want Java::Build. But, if you want
    control and the beauty of Perl, you might like Java::Build. Look for it
    on a CPAN mirror near you.

WARNINGS
    This is beta software. We are phasing in its use, but it is young.

    Java::Build is currently unix centric. We use it exclusively on Red Hat
    8. Please help me change this. If you want it to work on your platform,
    I'm sure we can work together to make that happen. Platform independence
    is one key advantage of Ant over make and other tools (including this
    one at present).

AUTHOR
    Phil Crow, <philcrow2000@yahoo.com>

LICENSE
    Same as Perl.




------------------------------

Date: Wed, 15 Oct 2003 08:15:47 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: check if a string ends with particular letters
Message-Id: <Xns94155E1701261sdn.comcast@216.196.97.136>

Helgi Briem <HelgiBriem_1@hotmail.com> wrote in 
news:od5qovcaggfmbga3gqqrvn0k926moip3ft@4ax.com:

> On Tue, 14 Oct 2003 16:15:40 -0500, "Eric J. Roode"
> <REMOVEsdnCAPS@comcast.net> wrote:
> 
>>> Could someone tell me how to check if a string ends with particular
>>> letters. For example, i want to check if the following string is
>>> ending with gif: 
>>> 
>>> "htt://www.mypage.com/myimage.gif"
>>
>>if (substr($string, -3) eq 'gif')
> 
> Have you succumbed to the troll disease?

Hahaha!  I wondered if someone would say something like that.  :-)

-- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print


------------------------------

Date: 15 Oct 2003 10:30:02 -0700
From: nobull@mail.com
Subject: Re: cleanup speed (named vs anonymous (and my?))
Message-Id: <4dafc536.0310150930.5aa3bfaf@posting.google.com>

Eric Wilhelm <ewilhelm@somethinglike.sbcglobalDOTnet> wrote in message news:<pan.2003.10.15.00.48.53.475835.13509@somethinglike.sbcglobalDOTnet>...
> I've been working on a 2D drafting / geometry module (CAD::Drawing) and 
> noticed some HUGE slowdown when working with extremely large hashes.  
> 
> This is an object-oriented module, so the typical blessed hash reference
> is returned by the constructor function:
> 
> sub new {
>   my $caller = shift;
>   my $class = ref($caller) || $caller;
>   my $self = {@_};
>   bless($self, $class);
>   return($self);
> }
> 
> The two keys under which most of the data is stored are $self->{g} (all
> geometry, nested by layer name and then entity) and $self->{colortrack}
> (keeping lists of addresses nested by layer,entity, then color.)
> 
> While loading the data into the structure took very little time with the
> above function, I knocked about 11 minutes off of the cleanup using the
> one below (from 11m27.723s down to 0m31.711s loading 689850 circles onto
> 4590 layers.
> 
> sub new {
>   my $caller = shift;
>   my $class = ref($caller) || $caller;
>   my $self = {@_};
>   # this is clunky, but saves -_#*HUGE*#_- on big drawings!
>   $CAD::Drawing::geometry_data{$self} = {};
>   $self->{g} = $CAD::Drawing::geometry_data{$self};
>   $CAD::Drawing::color_tracking{$self} = {};
>   $self->{colortrack} = $CAD::Drawing::color_tracking{$self};
>   bless($self, $class);
>   return($self);
> }
> 
> Is it documented somewhere that named references cleanup faster than
> anonymous ones?

I think the point is not that they are cleaned up faster but that the
package variables are simply not cleaned up at process end.

If you are happy for your class to leak memory the above trick will
work fine.

If you want Perl to terminate cleaning up at all then use the POSIX
_exit().

As I undersand it, package variables just continue to exist and the
memory will be freed when the process terminates.

Actually it's not just package variables but any variable that
referenced indirectly by the symbol table.

You would have seen the same effect if %geometry_data had been a
file-scoped lexical.

my $foo; # Will get cleaned up

my $bar; # Won't get cleaned up because...
sub uses_bar { $bar }; # ...$bar now referenced by &main::uses_bar

our $baz; # Won't get cleaned up because it's a package variable

my $bat; # Won't get cleaned up because...
our $uses_bat { $bar }; # ...$bar now referenced by $main::uses_bat

There is something called the global destruction phase when the
DESTROY() method is called for every object that's still left over at
the end of the process - but this is not a proper neat systematic
cleanup and even seemingly innocent things can actually segfault
during global destruction.

At least that's how I understand all this.  I've not actually tested
my beliefs extensively.


------------------------------

Date: Wed, 15 Oct 2003 12:50:26 -0400
From: slack3r <slack3r@nowhere.com>
Subject: Getting part of a number
Message-Id: <aeuqovopeq2sngh19hk16tqeerbns056e9@4ax.com>

I'm probably missing something simple but if you have a five digit zip
code, how would you go about getting just the first 3 digits?

Any help is greatly appreciated.


------------------------------

Date: Wed, 15 Oct 2003 17:03:54 +0000
From: Helgi Briem <HelgiBriem_1@hotmail.com>
Subject: Re: Getting part of a number
Message-Id: <mcvqov04vgr9br6lpnpudlp4082358nmfp@4ax.com>

On Wed, 15 Oct 2003 12:50:26 -0400, slack3r <slack3r@nowhere.com>
wrote:

>I'm probably missing something simple but if you have a five digit zip
>code, how would you go about getting just the first 3 digits?
>
>Any help is greatly appreciated.

my $zip = '12345';
my $zip_first_3 = substr($zip,0,3);
print $zip_first_3;


------------------------------

Date: Wed, 15 Oct 2003 17:01:21 GMT
From: Anand <some@one.com>
Subject: Re: Getting part of a number
Message-Id: <B3fjb.2107$8x2.1309173@newssrv26.news.prodigy.com>

$num = "12345";
$res = substr ($num, 0, 3);
print "$res\n";

where ,
  0 - is start of string you are loooking fro
  3 - number of digits you want.

--Anand

slack3r wrote:
> I'm probably missing something simple but if you have a five digit zip
> code, how would you go about getting just the first 3 digits?
> 
> Any help is greatly appreciated.



------------------------------

Date: Wed, 15 Oct 2003 13:26:07 -0400
From: slack3r <slack3r@nowhere.com>
Subject: Re: Getting part of a number
Message-Id: <1p0rovkhmjiam3qbeg9ds50bpo153j3ves@4ax.com>

Thank you both for all of your help


------------------------------

Date: Wed, 15 Oct 2003 15:53:12 +0100
From: Nick Wedd <nick@maproom.co.uk>
Subject: Getting values from a CGI call
Message-Id: <$aQsrhvY9Vj$EAaG@maproom.demon.co.uk>

I am writing a cgi script, and I want to get at the values which were
passed to it as arguments.  I finally found a way to do this, at
http://www.perldoc.com/perl5.8.0/lib/CGI.html.  You say
    use CGI qw/:standard/;
    my $edges = param('foo');
and it works.

But before I found this web page, I tried "Learning Perl", which
recommends (page 187)
    use CGI qw(:standard);
    my $edges  = param{"foo"};
This does not work.  Instead it sets $edges to "".

Then I tried "Perl in a Nutshell", which recommends (page 330)
    use CGI;
    my $edges = $query->param("foo");
This is worse.  My script stops at the second of the above lines.

Now I can't believe that these books are actually wrong about something
so basic.  Yet what they recommend does not work on my system.  Can
anyone explain what is going on here?

Nick
-- 
Nick Wedd    nick@maproom.co.uk


------------------------------

Date: Wed, 15 Oct 2003 11:02:43 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Nick Wedd <nick@maproom.co.uk>
Subject: Re: Getting values from a CGI call
Message-Id: <Pine.SGI.3.96.1031015110023.39267A-100000@vcmr-64.server.rpi.edu>

[posted & mailed]

On Wed, 15 Oct 2003, Nick Wedd wrote:

>But before I found this web page, I tried "Learning Perl", which
>recommends (page 187)
>    use CGI qw(:standard);
>    my $edges  = param{"foo"};
>This does not work.  Instead it sets $edges to "".

I doubt "Learning Perl" recommends using param {...}.  Those {}'s should
be ()'s, and if the book has {}'s, then it's a typo.

>Then I tried "Perl in a Nutshell", which recommends (page 330)
>    use CGI;
>    my $edges = $query->param("foo");
>This is worse.  My script stops at the second of the above lines.

Because you never created $query as a CGI object.

  use CGI;
  my $query = CGI->new;
  my $edges = $query->param('foo');

-- 
Jeff Pinyan            RPI Acacia Brother #734            2003 Rush Chairman
"And I vos head of Gestapo for ten     | Michael Palin (as Heinrich Bimmler)
 years.  Ah!  Five years!  Nein!  No!  | in: The North Minehead Bye-Election
 Oh.  Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)



------------------------------

Date: Wed, 15 Oct 2003 16:19:32 +0100
From: Nick Wedd <nick@maproom.co.uk>
Subject: Re: Getting values from a CGI call
Message-Id: <6eHtaYxEWWj$EAKv@maproom.demon.co.uk>

In message 
<Pine.SGI.3.96.1031015110023.39267A-100000@vcmr-64.server.rpi.edu>, Jeff 
'japhy' Pinyan <pinyaj@rpi.edu> writes

>>But before I found this web page, I tried "Learning Perl", which
>>recommends (page 187)
>>    use CGI qw(:standard);
>>    my $edges  = param{"foo"};
>>This does not work.  Instead it sets $edges to "".
>
>I doubt "Learning Perl" recommends using param {...}.  Those {}'s should
>be ()'s, and if the book has {}'s, then it's a typo.
>
>>Then I tried "Perl in a Nutshell", which recommends (page 330)
>>    use CGI;
>>    my $edges = $query->param("foo");
>>This is worse.  My script stops at the second of the above lines.
>
>Because you never created $query as a CGI object.
>
>  use CGI;
>  my $query = CGI->new;
>  my $edges = $query->param('foo');

Thank you, thank you.  I must learn to read more carefully.

Nick
-- 
Nick Wedd    nick@maproom.co.uk


------------------------------

Date: 15 Oct 2003 10:33:23 -0700
From: jorenders@hotmail.com (JR)
Subject: How can i write the values of a form through a cgi script in a txt file.
Message-Id: <4096148f.0310150933.706f3074@posting.google.com>

How can i write the values of a form through a cgi script in a txt file.

My html pages is for example:

<FORM METHOD=POST ACTION="script.cgi">
<input type="text" name="nummer" size="20"></p>
  <p><input type="text" name="paswoord" size="20"></p>
  <p><input type="submit" value="Submit" name="Send">
</form>

Can anybody help me with this simple script
I am a newbie and i would love to learn perl/cgi.


Thanks


------------------------------

Date: 15 Oct 2003 14:53:26 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: How does one move down a line in a file?
Message-Id: <slrnboqns9.fgj.xx087@smeagol.ncf.ca>

John <no@spam.here> wrote:
>  "Glenn Jackman" <xx087@freenet.carleton.ca> wrote in message...
> > James Willmore <jwillmore@remove.adelphia.net> wrote:
> > >  --untested--
> > >  while(<FILE>){
> > >      my $first = <FILE>;
> > >      my $second = <FILE>;
> > >      my $third = <FILE>;
> > >      last;
> > >  }
> > >  --untested--
> > >
> > >  This will get the first three lines - 'last' breaks the loop.
> >
> > That will get the 2nd, 3rd and 4th lines into $first, $second and $third
> > respectively.  Because they are my variables, they will be lost as soon
> > as you exit the while loop.
>  
>  wow, so many answers :)
>  
>  can some1 pls elaborate why the variables start getting values from the 2nd
>  line instead of the 1st one?
>  what happens to the first line? goes into a void?


This construct:  
    while (<FILE>) {...}
sets $_ each time through the loop.  So the first line is stored in $_,
which will remain after the while loop ends.

The perlop man page says:

     The following lines are equivalent:

         while (defined($_ = <STDIN>)) { print; }
         while ($_ = <STDIN>) { print; }
         while (<STDIN>) { print; }
         for (;<STDIN>;) { print; }
         print while defined($_ = <STDIN>);
         print while ($_ = <STDIN>);
         print while <STDIN>;

     This also behaves similarly, but avoids $_ :

         while (my $line = <STDIN>) { print $line }


I've forgotten your original question.  If it was how to read a file 3
lines at a time, you could write:
    
    while (my $one = <FILE>) {
        my $two = <FILE>;
        my $three = <FILE>;
        # do something with $one, $two, $three.
        # $two and $three may be undefined.
    }

-- 
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca


------------------------------

Date: 15 Oct 2003 09:51:15 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: How does one move down a line in a file?
Message-Id: <3ee08638.0310150851.4ee77f03@posting.google.com>

"John" <no@spam.here> wrote in message news:<Gy7jb.151558$bo1.15116@news-server.bigpond.net.au>...
> > James Willmore <jwillmore@remove.adelphia.net> wrote:
> > >  --untested--
> > >  while(<FILE>){
> > >      my $first = <FILE>;
> > >      my $second = <FILE>;
> > >      my $third = <FILE>;
> > >      last;
> > >  }
 ...
> can some1 pls elaborate why the variables start getting values from the 2nd
> line instead of the 1st one?
> what happens to the first line? goes into a void?

No, it goes into $_. What did you think the "while(<FILE>)" line is
doing?

Each time you use the <> operator, you grab a new line. Your original
example put the same grabbed line into each variable. The example
above has four reads, and the first one is basically ignored.

Reading the whole thing into an array (as suggested by Eric Roode) may
be your most straightforward bet, depending on what you are really
trying to do.

Here's a variation on Abigail's recommendation for getting lines into
three variables:

READLOOP:{
    defined($_=<DATA>) or last READLOOP for (my ($v1, $v2, $v3));

    # Process lines here

    redo;
}


------------------------------

Date: Wed, 15 Oct 2003 16:53:24 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: How does one move down a line in a file?
Message-Id: <20031015125342.67fce71f.jwillmore@remove.adelphia.net>

On 14 Oct 2003 17:47:56 GMT
Glenn Jackman <xx087@freenet.carleton.ca> wrote:

> James Willmore <jwillmore@remove.adelphia.net> wrote:
> >  --untested--
> >  while(<FILE>){
> >      my $first = <FILE>;
> >      my $second = <FILE>;
> >      my $third = <FILE>;
> >      last;
> >  }
> >  --untested--
> >  
> >  This will get the first three lines - 'last' breaks the loop.
> 
> That will get the 2nd, 3rd and 4th lines into $first, $second and
> $third respectively.  Because they are my variables, they will be
> lost as soon as you exit the while loop.

--untested--
my($first,$second,$third);
while(<FILE>){
    $first = <FILE>;
    $second = <FILE>;
    $third = <FILE>;
    last;
}
--untested--

-or-

--untested--
my($first,$second,$third);
my $count = 1;
while(<FILE>){
    $first = <FILE> if $count == 1;
    $second = <FILE> if $count == 2;
    $third = <FILE> if $count == 3;
    last if $count == 4;
}
--untested--

-or-

--untested--
my($first,$second,$third);
while(<FILE>){
    $first = <FILE> if $. == 1;
    $second = <FILE> if $. == 2;
    $third = <FILE> if $. == 3;
    last if $. == 4;
}
--untested--

-or-

--untested--
my @lines = <FILE>;
#access each ine as an element of the array
--untested--

-or-

use one of the _many_ Perl modules relating to files.

This question is a bit like "How do I print a formated line?".  The
accepted/first method is to use 'printf'.  However, depending on what
the end result is supposed to be and what else needs to be done and
how the output is _supposed_ to look, you could use 'format'.  Or, you
could use 'sprintf' to format the string first, then use'print' to
print the line (now containing the formated string).

Try writing out what you want to do on paper _first_.  The answer
_may_ just pop off the page at you.

HTH

-- 
Jim

Copyright notice: all code written by the author in this post is
 released under the GPL. http://www.gnu.org/licenses/gpl.txt 
for more information.

a fortune quote ...
Whistler's Law:  You never know who is right, but you always know
who is in charge. 


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 5663
***************************************


home help back first fref pref prev next nref lref last post