Johannes Eriksson wrote:
>
> perl -e 'rand;rand($.)<1&&($l=$_)while<>;print $l;' < file
>
> It works. Believe me.
i believe you. I just can't figure out what it is doing? I have been
working with perl lately and always looking to learn. Can you explain
what it is doing please?
this also appears to work:
perl -e 'rand($.)<1&&($l=$_)while<>;print $l;' < file
The $. is the current line from the last read filehandle,in the case
stdin, right? Does < file pass the file to the program or just the file
name?
That seems to imply we have read all the way through the file at least
once I think.
rand() will return a random number between 0 and the argument so will
this ever play the last file in the list if $. is the last line number?
I get lost pretty quick. the <1&&($l=$_)while<> contruct totally
baffles me other than knowing that while <> will read lines in from
stdin, $1=$_ will set variable $1 to the text of the current line but
beyond that I have no idea how it is all working together.
One of the issues I have with perl and experiencd perl programmers is
the tendency to create really compact and I guess efficient code such as
this one liner that are difficult to figure out for the less advanced of
us out here.
Anyway, help me learn to fish instead of just giving me a fish sandwich,
please. That way I can become enlightened and start writing code that
only I can debug :)
TIA Bret
_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list