Re: Perl realted question..
On Thu, 10 Jan 2008 13:47:41 -0900 Ken Irving <[EMAIL PROTECTED]> wrote: > On Wed, Jan 09, 2008 at 10:04:19PM -0500, ISHWAR RATTAN wrote: > > > > Is there a way to place the last line read > > when reading from a file? My suspicion is there > > is no such thing but i do want to confirm.. > > Just another guess at what you're asking about; perhaps the word > "placed" was intended to be "replaced"; i.e., can you replace a > line that you've just read? > > I think the general answer to that is "no", and the approach I've > always taken (using Perl) is to write a new file, copying the old > and adding new content, then rename and remove etc. when done. I > haven't delved into the internals much, though, and maybe there's > a way it could be done, but it hasn't been worth my time to do it. > If you think about a file stored on disk, text files have an arbitrary > structure defined by the end-of-line character(s). It seems like > it should be possible to swap out characters in a line, but the line > size would need to stay the same, perhaps using padding with spaces > or something. Probably much more reasonably and conventionally doable > with fixed-sized records, but if the need was there I don't see why > it shouldn't be possible. It is actually apparently quite simple (I've never tried it); from 'man perlfaq5': >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? > >Use the Tie::File module, which is included in the standard >distribution since Perl 5.8.0. BTW, the perl-beginners ML is a terrific place for perl help. It's read by some really helpful perl gurus who can and will answer pretty much any question you might have about perl. Just be aware that they will call you on not RTFM ... > Ken Celejar -- mailmin.sourceforge.net - remote access via secure (OpenPGP) email ssuds.sourceforge.net - A Simple Sudoku Solver and Generator -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
[OT] Re: Perl realted..?
pedxing <[EMAIL PROTECTED]>: > On Jan 11, 4:20 am, "s. keeling" <[EMAIL PROTECTED]> wrote: > > > > - > > #!/usr/bin/perl > > # this just re-implements tail -1 > > # > > # usage: > > # /this/file < /some/text/file.txt > > # > > > > my $last; > > > > while( <> ) { > > $last = $_; > > } > > > > print $last; > > - > > > > Now, try with the "my $last;" *inside* the while(). That last print > > line won't have a clue what $last is. > > my $last; > my $bar; > while( <> ){ >$last="foo"; >$bar=different_scope($_); > } > print $bar; > print $last; > > sub different_scope{ >my $last=$_[0]; >return $last; > } I haven't tested it. Saved for posterity though. > Actually, this is a better example, because it will actually work As does mine. > #;-). But the point is that scope doesn't change in a loop, but it > does in a subroutine. You should have tested my code. :-) Thanks to the others for their informative comments. -- Any technology distinguishable from magic is insufficiently advanced. (*)http://blinkynet.net/comp/uip5.html Linux Counter #80292 - -http://www.faqs.org/rfcs/rfc1855.htmlPlease, don't Cc: me.
Re: Perl realted..?
On Fri, Jan 11, 2008 at 01:51:44AM -0800, pedxing wrote: > But the point is that scope doesn't change in a loop, but it > does in a subroutine. You are incorrect. Every set of { braces } is its own (sub)scope, regardless of whether they are separated out into a sub or not. ~$ perl -w -e ' { my $last = "foo"; } print "$last\n"; ' Name "main::last" used only once: possible typo at -e line 1. Use of uninitialized value in concatenation (.) or string at -e line 1. ~$ perl -w -e ' my $last; { $last = "foo"; } print "$last\n"; ' foo Or, if you like the code from the original question better, try perl -w -e 'while (<>) { my $last = $_ } print "$last\n"; ' and it *will* complain about $last being undefined after the loop exits: ~$ perl -w -e 'while (<>) { my $last = $_ } print "$last\n"; ' < .bashrc Name "main::last" used only once: possible typo at -e line 1. Use of uninitialized value in concatenation (.) or string at -e line 1, <> line 51. -- I reckon we are now the only monastry ever that had a dungeon stuffed with sixteen thousand zombies. - perlmonks.org -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Perl realted..?
On Jan 10, 2008 1:23 PM, Chris Howie <[EMAIL PROTECTED]> wrote: > On Jan 10, 2008 1:11 PM, ISHWAR RATTAN <[EMAIL PROTECTED]> wrote: > > > I am coming back to perl after a long time. > > > > The sample code these days also uses variable attribute my as: > > > > my $inst = Extutils::Installed->new(); > > my @modules = $inst->modules(); > > > > Can any demistify 'my' for me?? > > > > -ishwar > > > > "my" declares a variable that exists within the current scope and is > unreachable from outside the scope. So a "my" declaration in one package > cannot be reached from another package. "our" is identical to "my" except > that you *can* reach that variable from other packages. A "my" declaration > inside of a function just limits the scope and lifetime of the variable to > that function. I just wanted to point out that to take full advantage of "my" you might want to program using "use strict" also. N.- -- http://arhuaco.org -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Perl realted..?
On Jan 11, 2008 4:01 AM, pedxing <[EMAIL PROTECTED]> wrote: > > #!/usr/bin/perl > > # this just re-implements tail -1 > > # > > # usage: > > # /this/file < /some/text/file.txt > > # > > > > my $last; > > > > while( <> ) { > > $last = $_; > > } > > > > print $last; > > - > > > > Now, try with the "my $last;" *inside* the while(). That last print > > line won't have a clue what $last is. > > That doesn't sound right to me. Scope doesn't change in a loop. All > of this code is in the same scope, so the print statement will work > fine. > Try again. 8< $ perl -le 'my $last = 1; while (true) { my $last = 2; last; } print $last;' 1 8< -- Chris Howie http://www.chrishowie.com http://en.wikipedia.org/wiki/User:Crazycomputers
Re: Perl realted..?
On Jan 11, 4:20 am, "s. keeling" <[EMAIL PROTECTED]> wrote: > ISHWAR RATTAN <[EMAIL PROTECTED]>: > > > I am coming back to perl after a long time. > > > The sample code these days also uses variable attribute my as: > > > my $inst = Extutils::Installed->new(); > > my @modules = $inst->modules(); > > > Can any demistify 'my' for me?? > > - > #!/usr/bin/perl > # this just re-implements tail -1 > # > # usage: > # /this/file < /some/text/file.txt > # > > my $last; > > while( <> ) { > $last = $_; > } > > print $last; > - > > Now, try with the "my $last;" *inside* the while(). That last print > line won't have a clue what $last is. > my $last; my $bar; while( <> ){ $last="foo"; $bar=different_scope($_); } print $bar; print $last; sub different_scope{ my $last=$_[0]; return $last; } Actually, this is a better example, because it will actually work #;-). But the point is that scope doesn't change in a loop, but it does in a subroutine. -pedxing
Re: Perl realted..?
On Jan 11, 4:20 am, "s. keeling" <[EMAIL PROTECTED]> wrote: > ISHWAR RATTAN <[EMAIL PROTECTED]>: > > > I am coming back toperlafter a long time. > > > The sample code these days also uses variable attribute my as: > > > my $inst = Extutils::Installed->new(); > > my @modules = $inst->modules(); > > > Can any demistify 'my' for me?? > > - > #!/usr/bin/perl > # this just re-implements tail -1 > # > # usage: > # /this/file < /some/text/file.txt > # > > my $last; > > while( <> ) { > $last = $_; > } > > print $last; > - > > Now, try with the "my $last;" *inside* the while(). That last print > line won't have a clue what $last is. > That doesn't sound right to me. Scope doesn't change in a loop. All of this code is in the same scope, so the print statement will work fine. If you move the loop to a subroutine, then the two $last variables will be independant due to separate scopes: my $last="foo"; $bar=scan_loop(); print $bar; print $last; sub scan_loop{ my $last; while( <> ){ $last=$_; return $last; } } This will print out the last line and then "foo" because there are two $last values, the one in the original scope and the one in the subroutine scope. -pedxing
Re: Perl realted..?
ISHWAR RATTAN <[EMAIL PROTECTED]>: > I am coming back to perl after a long time. > > The sample code these days also uses variable attribute my as: > > my $inst = Extutils::Installed->new(); > my @modules = $inst->modules(); > > Can any demistify 'my' for me?? - #!/usr/bin/perl # this just re-implements tail -1 # # usage: # /this/file < /some/text/file.txt # my $last; while( <> ) { $last = $_; } print $last; - Now, try with the "my $last;" *inside* the while(). That last print line won't have a clue what $last is. -- Any technology distinguishable from magic is insufficiently advanced. (*)http://blinkynet.net/comp/uip5.html Linux Counter #80292 - -http://www.faqs.org/rfcs/rfc1855.htmlPlease, don't Cc: me. -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Perl realted question..
On Wed, Jan 09, 2008 at 10:04:19PM -0500, ISHWAR RATTAN wrote: > > Is there a way to place the last line read > when reading from a file? My suspicion is there > is no such thing but i do want to confirm.. Just another guess at what you're asking about; perhaps the word "placed" was intended to be "replaced"; i.e., can you replace a line that you've just read? I think the general answer to that is "no", and the approach I've always taken (using Perl) is to write a new file, copying the old and adding new content, then rename and remove etc. when done. I haven't delved into the internals much, though, and maybe there's a way it could be done, but it hasn't been worth my time to do it. If you think about a file stored on disk, text files have an arbitrary structure defined by the end-of-line character(s). It seems like it should be possible to swap out characters in a line, but the line size would need to stay the same, perhaps using padding with spaces or something. Probably much more reasonably and conventionally doable with fixed-sized records, but if the need was there I don't see why it shouldn't be possible. Ken -- Ken Irving, [EMAIL PROTECTED] -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Perl realted question..
Dave Sherohman <[EMAIL PROTECTED]> writes: > On Wed, Jan 09, 2008 at 09:45:15PM -0800, Sam wrote: >> if i read you correctly, you can read the file into an array and use pop, >> which will return the last element read.Or you could use @array[-1] > > That's rather wasteful of memory, which becomes a concern with larger > files. If the objective is indeed to get the last line in the file, > then you only need a scalar to stick each line into and its final stored > value will be the last line: Or use File::ReadBackwards, which will generally do this in a much more efficient way. It's in Debian package libfile-readbackwards-perl. Scott. -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Perl realted..?
ISHWAR RATTAN escreveu: I am coming back to perl after a long time. The sample code these days also uses variable attribute my as: my $inst = Extutils::Installed->new(); my @modules = $inst->modules(); Can any demistify 'my' for me?? http://perldoc.perl.org/functions/my.html -- Atenciosamente, Arthur Furlan [EMAIL PROTECTED] -ishwar -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Perl realted..?
On Jan 10, 2008 1:11 PM, ISHWAR RATTAN <[EMAIL PROTECTED]> wrote: > I am coming back to perl after a long time. > > The sample code these days also uses variable attribute my as: > > my $inst = Extutils::Installed->new(); > my @modules = $inst->modules(); > > Can any demistify 'my' for me?? > > -ishwar > "my" declares a variable that exists within the current scope and is unreachable from outside the scope. So a "my" declaration in one package cannot be reached from another package. "our" is identical to "my" except that you *can* reach that variable from other packages. A "my" declaration inside of a function just limits the scope and lifetime of the variable to that function. -- Chris Howie http://www.chrishowie.com http://en.wikipedia.org/wiki/User:Crazycomputers
Re: Perl realted question..
On Wed, Jan 09, 2008 at 09:45:15PM -0800, Sam wrote: > if i read you correctly, you can read the file into an array and use pop, > which will return the last element read.Or you could use @array[-1] That's rather wasteful of memory, which becomes a concern with larger files. If the objective is indeed to get the last line in the file, then you only need a scalar to stick each line into and its final stored value will be the last line: #!/usr/bin/perl use strict; use warnings; my $last; while (<>) { $last = $_; chomp $last; } print $last, "\n"; Of course, if this is being done as a standalone operation, then just using `tail -1 filename` on the command line would definitely be easier and probably more efficient. -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Perl realted question..
if i read you correctly, you can read the file into an array and use pop, which will return the last element read.Or you could use @array[-1] On 1/9/08, Chris Howie <[EMAIL PROTECTED]> wrote: > > On Jan 9, 2008 10:04 PM, ISHWAR RATTAN <[EMAIL PROTECTED]> wrote: > > > Is there a way to place the last line read > > when reading from a file? My suspicion is there > > is no such thing but i do want to confirm.. > > > > What do you mean by "place?" > > -- > Chris Howie > http://www.chrishowie.com > http://en.wikipedia.org/wiki/User:Crazycomputers
Re: Perl realted question..
On Jan 9, 2008 10:04 PM, ISHWAR RATTAN <[EMAIL PROTECTED]> wrote: > Is there a way to place the last line read > when reading from a file? My suspicion is there > is no such thing but i do want to confirm.. > What do you mean by "place?" -- Chris Howie http://www.chrishowie.com http://en.wikipedia.org/wiki/User:Crazycomputers