Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

"uhshifting" the diamond?

by glwtta (Hermit)
on Oct 21, 2003 at 00:32 UTC ( [id://300797]=perlquestion: print w/replies, xml ) Need Help??

glwtta has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am doing something rather simple - iterating over the lines from <> and splitting each one into words. What I can't seem to figure out how to do, is grab the first two words from the first line, and then continue on as usual. In other words, I want to my $first_line = <>; my ($word1, $word2) = split /\s+/, $first_line; while(<>){...} but still have the rest of $first_line be processed in the wile(<>).

Am I missing something obvious?

(Oh, I wouldn't want to slurp the whole thing into RAM first, as the input can get rather largish)

Replies are listed 'Best First'.
Re: "uhshifting" the diamond?
by PodMaster (Abbot) on Oct 21, 2003 at 00:36 UTC
    `perldoc perlvar' => $.
    while(<>){ if( $. == 1 ){ warn "i'm splitting"; ... } ... }

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: "uhshifting" the diamond?
by pzbagel (Chaplain) on Oct 21, 2003 at 00:46 UTC

    How about a do..while() so that the read happens at the end of the loop instead of the beginning:

    #!/usr/bin/perl -w use strict; $_=<DATA>; (my ($first, $second), $_) = split(/\s+/, $_, 3); print "first: $first\n"; print "second: $second\n"; do{ print "rest: $_"; } while (<DATA>); __DATA__ This is line one! This is line two! This is line three! This is line four! This is line five!

    Outputs:

    first: This second: is rest: line one! rest: This is line two! rest: This is line three! rest: This is line four! rest: This is line five!

    HTH

Re: "uhshifting" the diamond?
by Abigail-II (Bishop) on Oct 21, 2003 at 09:58 UTC
    my $first_line = <>; my ($word1, $word2) = split /\s+/ => $first_line; $_ = $first_line; while (1) { # Your loop. $_ = <> err last; # Defined-or! }

    Abigail

Re: "uhshifting" the diamond?
by Limbic~Region (Chancellor) on Oct 21, 2003 at 00:38 UTC
    glwtta,
    If this were a regular file, you could seek back to the beginning of the file. See perldoc -f seek. PodMaster was gracious enough to point out in a /msg that this would not work on a tty or a pipe.

    So this leaves handling the first line differently. This can be accomplished using a "flag" or using $. as PodMaster pointed out. That pretty makes this post pointless other than to show what you could do if this was a regular file.

    Cheers - L~R

Re: "uhshifting" the diamond?
by delirium (Chaplain) on Oct 21, 2003 at 02:43 UTC
    You could move the meat of the while loop to another place, like so:

    sub some_function { # do stuff with $_ } my ($word1, $word2) = split /\s+/,<>; &some_function; # $_ is the first line &some_function while (<>); # $_ cycles through rest of file

    ...and if you dislike egregiously abusing $_, you can throw in explicit variables in some_function.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://300797]
Approved by PodMaster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-25 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found