[22534] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4755 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 24 18:05:44 2003

Date: Mon, 24 Mar 2003 15:05:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 24 Mar 2003     Volume: 10 Number: 4755

Today's topics:
        Algorithm for Text Subsets <delautenschl@[nospam]wisc.edu>
    Re: Algorithm for Text Subsets (Anno Siegel)
    Re: Editor with Perl as macro language? <Norbert_Schmidt@DU3.MAUS.DE>
    Re: Editor with Perl as macro language? <Norbert_Schmidt@DU3.MAUS.DE>
    Re: Editor with Perl as macro language? <Norbert_Schmidt@DU3.MAUS.DE>
    Re: Newbie needs help: Checking input after blank line (Anno Siegel)
        News posts with attachments <bigus NO @ SPAM creationfactor .net>
    Re: News posts with attachments <bigus NO @ SPAM creationfactor .net>
        pattern matching <harnoor@seas.ucla.edu>
    Re: pattern matching <bigus NO @ SPAM creationfactor .net>
    Re: pattern matching <perl-dvd@darklaser.com>
    Re: pattern matching <harnoor@seas.ucla.edu>
    Re: Perl ODBC and SQL <tore@aursand.no>
    Re: pointing stderr to a module <Norbert_Schmidt@DU3.MAUS.DE>
    Re: Really weird?!? <please@no.spam>
        Simple, but I can't figure it out. (Corey Andrews)
    Re: Simple, but I can't figure it out. <bigus NO @ SPAM creationfactor .net>
    Re: Simple, but I can't figure it out. <thepoet@nexgo.de>
    Re: Simple, but I can't figure it out. <tore@aursand.no>
    Re: socket- and pseudo-tty-problem <michael@binaervarianz.de>
    Re: update value in flat file (Anno Siegel)
    Re: update value in flat file <noreply@gunnar.cc>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 24 Mar 2003 15:15:50 -0600
From: pooba53 <delautenschl@[nospam]wisc.edu>
Subject: Algorithm for Text Subsets
Message-Id: <b5nsi4$40i$1@news.doit.wisc.edu>

I have a portion of a large Perl program where I have a hash with
multiple values associated with each key. Lets say I have the following
values associated with a key called "Gene":

1. actgattgaacca
2. gattg
3. actggtaaccggttaacctt

The number of values varies from key to key, by the way.

What I am trying to do with the above group is to eliminate any line of
text whose pattern is contained in another. In the above example line
number 2 would be removed because its pattern is found in the middle of
line 1.

The problem I'm having is figuring our how you compare each line to
every other line, move to the next line, compare with all the rest,
etc., etc...

Thanks for the help.

-Dan


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

Date: 24 Mar 2003 21:54:33 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Algorithm for Text Subsets
Message-Id: <b5nuqp$sid$3@mamenchi.zrz.TU-Berlin.DE>

pooba53  <delautenschl@[nospam]wisc.edu> wrote in comp.lang.perl.misc:
> I have a portion of a large Perl program where I have a hash with
> multiple values associated with each key. Lets say I have the following
> values associated with a key called "Gene":
> 
> 1. actgattgaacca
> 2. gattg
> 3. actggtaaccggttaacctt
> 
> The number of values varies from key to key, by the way.
> 
> What I am trying to do with the above group is to eliminate any line of
> text whose pattern is contained in another. In the above example line
> number 2 would be removed because its pattern is found in the middle of
> line 1.
> 
> The problem I'm having is figuring our how you compare each line to
> every other line, move to the next line, compare with all the rest,
> etc., etc...

Do it the other way round.  First sort the strings descending by length.
Start with an empty set of strings to keep.  For each original string,
see if it is contained in one of the strings you have already collected.
If it isn't, add it to the collection, else drop it.  In code:

    my @strings = sort { length $b <=> length $a }
        qw( actgattgaacca gattg actggtaaccggttaacctt);

    my @keep;
    for my $cand ( @strings ) {
        push @keep, $cand unless grep 1 + index( $_, $cand), @keep;
    }

Note that index() returns -1 if the substring can't be found.

Anno


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

Date: Mon, 24 Mar 2003 22:10:56 +0100
From: Norbert Schmidt <Norbert_Schmidt@DU3.MAUS.DE>
Subject: Re: Editor with Perl as macro language?
Message-Id: <jqsu7vg0ro2d2hp18s63tbi27omu98r29q@4ax.com>

Hello Randal,

>You can get the best/worst of both worlds by "perl-enabling" your
>Emacs build.  I'm not sure if they've kept up the latest patches, but
>it looked pretty cool at one point.  

from what I could see, it's still beta and has been unchanged for 3
years ... :-(

And no Windows version.


Regards, Norbert


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

Date: Mon, 24 Mar 2003 22:47:49 +0100
From: Norbert Schmidt <Norbert_Schmidt@DU3.MAUS.DE>
Subject: Re: Editor with Perl as macro language?
Message-Id: <9atu7vos6qi8rit788ib47ru9korgv0dbe@4ax.com>

Hello Malcolm,

thanks for your suggestions. But it's not quite, what I was looking
for. ;-)

>In windows, in your editor use Edit:Copy and then switch to a command
>windows and run a perl script that modifies the paste buffer, and then
>switch back to the editor and do Edit:Paste to put the modified text back
>into the editor. 

I already execute perl scripts via the shell command of my old DOS
editor. Works fine for some complex tasks. I just don't like the time
it takes to start the interpreter each time a script is executed.  

I was dreaming of an editor that I can customize by simply writing
packages that inherit from basic editor classes. And I would just have
to override some methods or write new ones and assign them to keys.
And bind such a substitute class to buffers or file types. Or
something like that. I.e. I'ld really like something vaguely like
emacs, but based on perl. :-)

But I'ld settle for an editor where I can assign perl functions to
keys and maybe replace built in functions by own versions.

Regards, Norbert


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

Date: Mon, 24 Mar 2003 23:22:46 +0100
From: Norbert Schmidt <Norbert_Schmidt@DU3.MAUS.DE>
Subject: Re: Editor with Perl as macro language?
Message-Id: <jj0v7vsrc9qma18u0h1q4n9ambfo1c4nu9@4ax.com>

Hello Tim,

>At work we primarily use a program called Codewright.  It's quite
>powerful and every install comes with a built-in perl
>distribution and a custom CWP.pm perl module to call codewright
>API functions.

thanks for the information! I'll have a look at it.


>Unfortunately, Codewright is commercial software and, though I've
>never checked myself, I believe it's in the $200-$300 (US) range.

While I prefer free software where I could-in an emergency-find a bug
in the source, I don't mind paying for something that works. ;-)


>vim+perl binary packages available for both linux and
>Microsoft(R) Windows(R). 

Maybe if I can hide some of the vi style keyboard assignment ... ;-)

Regards, Norbert


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

Date: 24 Mar 2003 21:31:45 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Newbie needs help: Checking input after blank line
Message-Id: <b5ntg1$sid$2@mamenchi.zrz.TU-Berlin.DE>

Bex <be_insane@yahoo.com> wrote in comp.lang.perl.misc:
> Hi People, 

[...]

> What i would like to do is specifcy that after the first blank line is
> detected all text from that point onwards (whether another blank line
> or not) is selected, and outputted.

    not 1 .. /^$/ and print while <DATA>;

Anno



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

Date: Mon, 24 Mar 2003 21:27:17 -0000
From: "Bigus" <bigus NO @ SPAM creationfactor .net>
Subject: News posts with attachments
Message-Id: <oHKfa.847$wi1.162@newsfep4-winn.server.ntli.net>

Hi

If I download the message body of a news post, using NNTPClient module, and
it has an attachment - for example a JPG image file or an M$ Word document,
how can I turn the attachment back into a proper file?

For example, here is the body of a message that had an image attached
(between the dashed lines):

--------------------------
here is my cat

begin 666 DSCF0002.JPG
M_]C_X2ZO17AI9@``24DJ``@````+``\!`@`)````D@```! !`@`0````G ``
M`!(!`P`!`````0!%0AH!!0`!````K ```!L!!0`!````M ```"@!`P`!````
[..]
M^IVX^O\`DU6^V1'HRG\1_P#7H"Q9,B8S@9JH;F <D#(].?Z4"+)=B<8/Y<55
H^UH1W(],4#L6&WJ<X5A53[5"<]=WI1H%BWYB'TSZ51-XG]UA0%C_V0``
`
end

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

I presume there must be some generic way of doing it, since a newsreader
can't possibly have filters for every type of file in existance and yet any
type of file attached to a message will show as an attachment, if you see
what I mean.. is it somehow converting the file back into a binary format?
How could I do that in Perl?

Thanks

Bigus




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

Date: Mon, 24 Mar 2003 22:05:56 -0000
From: "Bigus" <bigus NO @ SPAM creationfactor .net>
Subject: Re: News posts with attachments
Message-Id: <EfLfa.883$wi1.747@newsfep4-winn.server.ntli.net>

Darn - I just noticed that Outlook, if you are using that, actually thought
there was an attachment in the code I attemtped to quote.. doh!

Let me try again:

--------------------------
#here is my cat
#
#begin 666 DSCF0002.JPG
#M_]C_X2ZO17AI9@``24DJ``@````+``\!`@`)````D@```! !`@`0````G ``
#M`!(!`P`!`````0!%0AH!!0`!````K ```!L!!0`!````M ```"@!`P`!````
[..]
#M^IVX^O\`DU6^V1'HRG\1_P#7H"Q9,B8S@9JH;F <D#(].?Z4"+)=B<8/Y<55
#H^UH1W(],4#L6&WJ<X5A53[5"<]=WI1H%BWYB'TSZ51-XG]UA0%C_V0``
#`
#end
#
--------------------------

The hashes at the beginning of each line weren't in the original message
body.

Bigus


Bigus wrote:
>> Hi
>>
>> If I download the message body of a news post, using NNTPClient
>> module, and it has an attachment - for example a JPG image file or
>> an M$ Word document, how can I turn the attachment back into a
>> proper file?
>>
>> For example, here is the body of a message that had an image attached
>> (between the dashed lines):
>>
>> --------------------------
>> here is my cat
>>
>>
>
>
>
>>
>> --------------------------
>>
>> I presume there must be some generic way of doing it, since a
>> newsreader can't possibly have filters for every type of file in
>> existance and yet any type of file attached to a message will show
>> as an attachment, if you see what I mean.. is it somehow converting
>> the file back into a binary format? How could I do that in Perl?
>>
>> Thanks
>>
>> Bigus




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

Date: Mon, 24 Mar 2003 13:37:50 -0800
From: Arun Harnoor <harnoor@seas.ucla.edu>
Subject: pattern matching
Message-Id: <Pine.GSO.4.44.0303241331360.13672-100000@riverside>



I am a newbie to perl...I have an array of words - and I need to do a
pattern match/substitution of these words on this line...something like:
for($i=0;$i<Num_words;$i++)
{
	$line=~s/$word[$i]/$newword[$i]/;
}

I know that the above line won't work-- but it conveys best, what I want.
essentially my pattern is a variable....can someone help?

Thank you,

Arun



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

Date: Mon, 24 Mar 2003 22:00:40 -0000
From: "Bigus" <bigus NO @ SPAM creationfactor .net>
Subject: Re: pattern matching
Message-Id: <IaLfa.875$wi1.343@newsfep4-winn.server.ntli.net>

Arun Harnoor wrote:
> I am a newbie to perl...I have an array of words - and I need to do a
> pattern match/substitution of these words on this line...something
> like: for($i=0;$i<Num_words;$i++)
> {
> $line=~s/$word[$i]/$newword[$i]/;
> }
>
> I know that the above line won't work-- but it conveys best, what I
> want. essentially my pattern is a variable....can someone help?

This would work:

$sentence = "The boy walked down the road and found an apple";
%words = (apple=>orange,boy=>girl,road=>street);
foreach(keys %words){
    if($sentence =~ /\b$_\b/){$sentence =~ s/\b$_\b/$words{$_}/g;}
}
print $sentence;

That is case sensitive. If you don't want the match to be case sensitive,
then the main line should read:

if($sentence =~ /\b$_\b/i){$sentence =~ s/\b$_\b/$words{$_}/gi;}

Note the "i"'s strategically placed :)

Bigus




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

Date: Mon, 24 Mar 2003 21:57:19 GMT
From: "David" <perl-dvd@darklaser.com>
Subject: Re: pattern matching
Message-Id: <3bLfa.11$xC2.6228@news-west.eli.net>

"Arun Harnoor" <harnoor@seas.ucla.edu> wrote in message
news:Pine.GSO.4.44.0303241331360.13672-100000@riverside...
> I am a newbie to perl...I have an array of words - and I need to do a
> pattern match/substitution of these words on this line...something
like:
> for($i=0;$i<Num_words;$i++)
> {
> $line=~s/$word[$i]/$newword[$i]/;
> }
>
> I know that the above line won't work-- but it conveys best, what I
want.
> essentially my pattern is a variable....can someone help?

Looks like you were pretty close to your solution, how about this:

for($i = 0; $i < $Num_words; $i++) {
$line=~s/$word[$i]/$newword[$i]/g;
}

Ok, really all I've done here is put a $ for your Num_words var, and add
a "g" (for global, match as many as you can find) to the end of your
regex.  Assuming $line and $Num_words contain what you think they
contain, and your arrays @word and @newword contain what you think they
do, this should work.

Regards,
David
perl -e'print for map chr$_+1,"111100113107044099117099132"=~/(.{3})/g'




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

Date: Mon, 24 Mar 2003 14:04:16 -0800
From: Arun Harnoor <harnoor@seas.ucla.edu>
To: David <perl-dvd@darklaser.com>
Subject: Re: pattern matching
Message-Id: <Pine.GSO.4.44.0303241403220.14791-100000@riverside>

Thank you for the replies...I was actually right-- the problem seems to be
elsewhere.

--Arun

On Mon, 24 Mar 2003, David wrote:

> "Arun Harnoor" <harnoor@seas.ucla.edu> wrote in message
> news:Pine.GSO.4.44.0303241331360.13672-100000@riverside...
> > I am a newbie to perl...I have an array of words - and I need to do a
> > pattern match/substitution of these words on this line...something
> like:
> > for($i=3D0;$i<Num_words;$i++)
> > {
> > $line=3D~s/$word[$i]/$newword[$i]/;
> > }
> >
> > I know that the above line won't work-- but it conveys best, what I
> want.
> > essentially my pattern is a variable....can someone help?
>
> Looks like you were pretty close to your solution, how about this:
>
> for($i =3D 0; $i < $Num_words; $i++) {
> $line=3D~s/$word[$i]/$newword[$i]/g;
> }
>
> Ok, really all I've done here is put a $ for your Num_words var, and add
> a "g" (for global, match as many as you can find) to the end of your
> regex.  Assuming $line and $Num_words contain what you think they
> contain, and your arrays @word and @newword contain what you think they
> do, this should work.
>
> Regards,
> David
> perl -e'print for map chr$_+1,"111100113107044099117099132"=3D~/(.{3})/g'
>
>
>

=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4
Arun Harnoor

=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=A4=
=A4



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

Date: Mon, 24 Mar 2003 23:56:49 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: Perl ODBC and SQL
Message-Id: <pan.2003.03.24.22.39.05.514413@aursand.no>

On Mon, 24 Mar 2003 17:42:55 +0000, Michael Budash wrote:
> my $sth = $dbh->prepare('SELECT *
>                          FROM userstable
>                          WHERE user = ' . $dbh->quote($nickname) . ' 
>                          ORDER BY thedate DESC');

Warning:  bind()'ing parameters is a _far_ better method! :)


-- 
Tore Aursand


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

Date: Mon, 24 Mar 2003 21:03:05 +0100
From: Norbert Schmidt <Norbert_Schmidt@DU3.MAUS.DE>
Subject: Re: pointing stderr to a module
Message-Id: <7rou7vco07pbhln13bg6kag8q1rreh5b2h@4ax.com>

>So, I am wondering if I can redirect STDERR to the LogModule's do_log
>function?

Have a look at how CGI::Carp.pm does it.
Or try Tie::HANDLE. (perltie documentation)



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

Date: Mon, 24 Mar 2003 21:16:52 +0000
From: Chris Lowth <please@no.spam>
Subject: Re: Really weird?!?
Message-Id: <mCKfa.2331$ob3.894@newsfep1-gui.server.ntli.net>

Anno Siegel wrote:

> Chris Lowth  <please@no.spam> wrote in comp.lang.perl.misc:
>> Michael Budash wrote:
>> 
>> > In article <FHmfa.48$%l1.240@news13-win.server.ntlworld.com>,
>> >  Chris Lowth <please@no.spam> wrote:
>> >> Michael Budash wrote:
>> >> > In article <nh6fa.7$v6.24796@newsfep2-gui>,
>> >> >  Chris Lowth <please@no.spam> wrote:
>> >> >> Ryan Ritten wrote:
>> >> >> 
>> >> >> > I don't get it... all I want is to check to see if the string
>> >> >> > $key contains the string of characters represented by
>> >> >> > table$table!
>> >> >> > 
>> >> >> > so for example... if $table was equal to 4 .... it would check
>> >> >> > $key and see if the string table4! was found within it... here is
>> >> >> > my code:
>> >> >> > 
>> >> >> > 
>> >> >> > print "key=$key\n";
>> >> >> > print "table=$table\n";
>> >> >> > if($key =~ /table$table!/)
>                      ^            ^
> [...]
> 
>> Correct! - and if you note, the posters original script was missing those
>> slashes which is why I suggested correcting the syntax in the first
>> place.
> 
> Hmmm...
> 
> Anno

Well, there is something stranger than fiction then - here's the first few 
lines of the original post exactly as they appear in "knode" on RedHat 8.0 
(look - no slashes!) ....

-------------- snip -----------------

I don't get it... all I want is to check to see if the string $key
contains the string of characters represented by table$table!

so for example... if $table was equal to 4 .... it would check $key
and see if the string table4! was found within it... here is my code:


print "key=$key\n";
print "table=$table\n";
if($key =~ table$table!)
{
        print "I'm in\n";
}

if I run this... for example... I'd get :

key=blahtable4!
table=4

------------ unsnip --------------

I have checked and double checked, and the slashes really are missing when 
"knode" displays the post, and yet when I check on google they are there. 
So forgive me - you appear to be right. Although I am mystified about 
knode's behaviour here.

Oddly Michael's last post also looked like this...

------- snip ------
> $table = 4;
> print "key=$key\n";
> print "table=$table\n";
> if($key =~  table$table! )
             ^            ^
             ^            ^
uh, notice anything missing above? somehow, the forward slashes 
surrounding the regex disappeared, causing the syntax error mentioned 
below. the code with forward slashes passes syntax check.
----- unsnip -----

So you can understand why I was confused by your comments, and why you were 
confused by mine !!!

So -- sorry guys! I need to check out the RedHat 8 version of "knode" to 
find out why I am getting this going on. Mean time - please accept my 
humble pie eating over this one.  :-)

Let's get back to perl stuff!

All the best

Chris

-- 
My real address is: chris at lowth dot sea oh em
-> OpenSource e-mail virus protection : http://protector.sourceforge.net
-> iptables configuration wizards : http://www.lowth.com/LinWiz


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

Date: 24 Mar 2003 14:17:56 -0800
From: matirxrabbit@Hotmail.com (Corey Andrews)
Subject: Simple, but I can't figure it out.
Message-Id: <8e213620.0303241417.7ca82de2@posting.google.com>

Okay guys, here's the problem. I'm a newbie and I'm not too familiar
with all the functions/methods of perl, so scripting cgi to something
of my liking is quite difficult. What I've been working on though, is
to create a script that will read and print out the first 20 lines of
a file.

Here's the scenario. I am creating news posting script, and every time
someone enters in a new news entry to a file, I have it so the news
entry will be assigned a different # each time, and that # will write
out to the beginning of a separate file, thus, the most #s being at
the beginning of the file, and the oldest, at then end. Now I just
want to create a script that will display the archive of news #s, but
only the most recent (first 20). So, I just need to print out the
first 20 lines of the file.  But only 20 lines, so every time I add a
new #, the last # gets booted out.

Very simple thing right? Well I can't figure out how to do it. I've
been experimenting with the for loops and such, but I'm lost. Any
help? Or just help on where to get started?


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

Date: Mon, 24 Mar 2003 22:36:54 -0000
From: "Bigus" <bigus NO @ SPAM creationfactor .net>
Subject: Re: Simple, but I can't figure it out.
Message-Id: <GILfa.921$wi1.149@newsfep4-winn.server.ntli.net>

Corey Andrews wrote:
> Okay guys, here's the problem. I'm a newbie and I'm not too familiar
> with all the functions/methods of perl, so scripting cgi to something
> of my liking is quite difficult. What I've been working on though, is
> to create a script that will read and print out the first 20 lines of
> a file.
>
> Here's the scenario. I am creating news posting script, and every time
> someone enters in a new news entry to a file, I have it so the news
> entry will be assigned a different # each time, and that # will write
> out to the beginning of a separate file, thus, the most #s being at
> the beginning of the file, and the oldest, at then end. Now I just
> want to create a script that will display the archive of news #s, but
> only the most recent (first 20). So, I just need to print out the
> first 20 lines of the file.  But only 20 lines, so every time I add a
> new #, the last # gets booted out.
>
> Very simple thing right? Well I can't figure out how to do it. I've
> been experimenting with the for loops and such, but I'm lost. Any
> help? Or just help on where to get started?

Assuming I understand what you want correctly, and making the example 3
instead of 20 for convenience, you could do this:

Say in the file "news.txt" you had the following lines:

23
22
21

Open the file and read the lines into an array:

open(TMP,"news.txt");
@lines = <TMP>;
close(TMP);

So, you wanna write the new news article no. 24 to the beginning of the file
and then the first two lines of the array. Easy, just do this:

open(TMP,">news.txt");
print TMP "24\n";
for($i = 0;$i <=1;$i++){
    print TMP "$lines[$i]\n";
}
close(TMP);

Your files will now read:

24
23
22

I've probably completely misunderstood you, but maybe that will help anyway
;-)

Bigus




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

Date: Mon, 24 Mar 2003 23:44:48 +0100
From: "Christian Winter" <thepoet@nexgo.de>
Subject: Re: Simple, but I can't figure it out.
Message-Id: <3e7f8a81$0$9925$9b4e6d93@newsread2.arcor-online.net>

"Corey Andrews" <matirxrabbit@Hotmail.com> wrote:
[...]
> 
> Here's the scenario. I am creating news posting script, and every time
> someone enters in a new news entry to a file, I have it so the news
> entry will be assigned a different # each time, and that # will write
> out to the beginning of a separate file, thus, the most #s being at
> the beginning of the file, and the oldest, at then end. Now I just
> want to create a script that will display the archive of news #s, but
> only the most recent (first 20). So, I just need to print out the
> first 20 lines of the file.  But only 20 lines, so every time I add a
> new #, the last # gets booted out.

Something like
----------------------------------
open(I,"< newfile.txt) or die $!;
while( <I> ) {
  print;
  last if( $. == 20 );
}
close I;
-----------------------------------
?

$. is the line number of the last accessed
file handle, "perldoc -f eof" has an example
that illustrates that, "perldoc perlvar" gives
details.

HTH
-Christian



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

Date: Mon, 24 Mar 2003 23:56:44 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: Simple, but I can't figure it out.
Message-Id: <pan.2003.03.24.22.53.51.95697@aursand.no>

On Mon, 24 Mar 2003 14:17:56 -0800, Corey Andrews wrote:
> Subject: Simple, but I can't figure it out.

Please post the _subject_ of your post in the subject of your post, not
how _you_ feel about the post.

> I'm a newbie and I'm not too familiar with all the functions/methods of
> perl [...]

But you've read the documentation available, right?

> What I've been working on though, is to create a script that will read
> and print out the first 20 lines of a file.

There are many, many, many (ah, Police Academy) ways to do this.  I'm
quite surprised that you haven't been able to solve it yourself.  You say
that you've been experimenting with "for loops and such" -- have you
looked at the splice() function?

Two easy methods to get you going;

  my @lines = <FILE>;
  print split( @lines, 0, 20 );

Or - memory friendly:

  while ( <FILE> ) {
      last if $. == 21;
      print;
  }

-- 
Tore Aursand


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

Date: Mon, 24 Mar 2003 20:53:52 +0100
From: Michael Pradel <michael@binaervarianz.de>
Subject: Re: socket- and pseudo-tty-problem
Message-Id: <b5nnlv$8jn$05$1@news.t-online.com>

 > I think your terminal is probably in canonical mode.  This means input
 > is processed by line, not character, so getc() only begins to read
 > characters once EOL is received.
 >
 >   2) eyrie:~% perl -wle 'while (1) { print ord getc(STDIN) }'
 >   input
 >   105
 >   110
 >   112
 >   117
 >   116
 >   10
 >   ^C
 >
 > There are ways to turn off canonical mode.  Read about stty(1) and the
 > POSIX module.

I use 'ReadMode(4)' from the module Term::Readkey. This should set the 
terminal in 'raw mode'. I also tried 'ReadMode(3)', which means 'cbreak 
mode'. I thought both are non-canonical (?).

I also tried it with a 'system("stty -icanon")' instead, but it also 
didn't work.

Any other ideas?

 > Finally, you're in for a world of hurt if you let anyone with a telnet
 > client run a shell on your machine.  Seriously consider using sshd
 > instead.

Oh yes - i know. That's only for testing.



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

Date: 24 Mar 2003 20:16:13 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: update value in flat file
Message-Id: <b5np2d$qch$1@mamenchi.zrz.TU-Berlin.DE>

Gunnar Hjalmarsson  <noreply@gunnar.cc> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Gunnar Hjalmarsson  <noreply@gunnar.cc> wrote in comp.lang.perl.misc:
> >>Unless you don't have Perl v5.8, I suggest that you ignore the FAQ 
> >>answer, and use the Tie::File module instead.
> > 
> > Have you seen the answer in the 5.8.0 FAQ?  It says:
> > 
> >                Use the Tie::File module, which is included in the
> >                standard distribution since Perl 5.8.0.
> 
> Yes, I had seen it, and my point is that it differs from the answer in 
> the 5.6.1 FAQ. If OP is using 5.6.1 or previous, he won't get the 
> Tie::File recommendation by checking the FAQ.

Oh, I see.  Yes, apparently it should run with older perls.

Though I'd often prefer people who ask this question did it the old
way and *learn*, on that occasion, what's involved in "changing a line
in a file".  Then let them tie their files.

Anno


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

Date: Mon, 24 Mar 2003 23:10:28 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: update value in flat file
Message-Id: <b5nvp2$2ba3fp$1@ID-184292.news.dfncis.de>

Anno Siegel wrote:
> Gunnar Hjalmarsson  <noreply@gunnar.cc> wrote in comp.lang.perl.misc:
>>If OP is using 5.6.1 or previous, he won't get the 
>>Tie::File recommendation by checking the FAQ.
> 
> Oh, I see.  Yes, apparently it should run with older perls.

Yes, 5.005 and above.

> Though I'd often prefer people who ask this question did it the old
> way and *learn*, on that occasion, what's involved in "changing a line
> in a file".  Then let them tie their files.

Oh, "reinvent the wheel" you mean? Sure, I agree that it's wise to get 
a basic understanding about changing a line before using such a nifty 
module.

Btw, would you agree that that approach is advisable also when it 
comes to CGI? In other words, that just "use CGI;" isn't necessarily 
the best answer to every CGI related question? ;-)

/ Gunnar

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

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 4755
***************************************


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