Controlling line length read by <> 
Author Message
 Controlling line length read by <>

Hi,

        Is it possible to make the <> ignore lines which contain more than
an arbitrary amount of characters when reading a file (or any other
source of data)?

Regards,
Bernard
--
perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
print split/No such file or directory/;'



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

to say:

Quote:

> Hi,

>    Is it possible to make the <> ignore lines which contain more than
> an arbitrary amount of characters when reading a file (or any other
> source of data)?

> Regards, Bernard
> --
> perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
> print split/No such file or directory/;'

perlfoc -f substr

- or -

perldoc -f read

--
perl -e 'print `echo Just a Lame Perl Luser | gzip -9 | cat | gzip -cd`'



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:


>to say:

>> Hi,

>>        Is it possible to make the <> ignore lines which contain more than
>> an arbitrary amount of characters when reading a file (or any other
>> source of data)?

>> Regards, Bernard
>> --
>> perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
>> print split/No such file or directory/;'

>perlfoc -f substr

I think substr would require the whole line to be known so it would
still read an entire line (I could be wrong), but...

Quote:
>- or -

>perldoc -f read

...this looks like what I'm looking for. Thanks very much.

Cheers,
Bernard
--
perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
print split/No such file or directory/;'



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

to say:

Quote:
>>perlfoc -f substr

> I think substr would require the whole line to be known so it would
> still read an entire line (I could be wrong), but...

>>- or -

>>perldoc -f read

> ...this looks like what I'm looking for. Thanks very much.

.. if you only want to read one line

On the next 'read' you will not get the beginning of the next line,
you'll get the rest of the line ...

--
perl -e 'print `echo Just a Lame Perl Luser | gzip -9 | cat | gzip -cd`'



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:


>to say:

>>>perlfoc -f substr

>> I think substr would require the whole line to be known so it would
>> still read an entire line (I could be wrong), but...

>>>- or -

>>>perldoc -f read

>> ...this looks like what I'm looking for. Thanks very much.

>.. if you only want to read one line

>On the next 'read' you will not get the beginning of the next line,
>you'll get the rest of the line ...

That's right. And that's a problem I ran into when toying with 'read'
just now. Is there any way to read only n-bytes of each line read by <>?

Cheers,
Bernard
--
perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
print split/No such file or directory/;'



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:

> Hi,

>         Is it possible to make the <> ignore lines which contain more than
> an arbitrary amount of characters when reading a file (or any other
> source of data)?

> Regards,
> Bernard
> --
> perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
> print split/No such file or directory/;'

my $MAX=1024; # kbyte

while (<>) {

  next if length > $MAX;

  # otherwise process the line

Quote:
}



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:


>> Hi,

>>         Is it possible to make the <> ignore lines which contain more than
>> an arbitrary amount of characters when reading a file (or any other
>> source of data)?

>> Regards,
>> Bernard
>> --
>> perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
>> print split/No such file or directory/;'

>my $MAX=1024; # kbyte

>while (<>) {

>  next if length > $MAX;

>  # otherwise process the line

>}

Obviously that's not what I'm looking for. I want to entirely skip lines
which are too long to avoid my program chocking if someone tries to pass
it 100MB in one line (extreme example, I admit :-)

Cheers,
Bernard
--
perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
print split/No such file or directory/;'



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

to say:

Quote:
> Obviously that's not what I'm looking for. I want to entirely skip lines
> which are too long to avoid my program chocking if someone tries to pass
> it 100MB in one line (extreme example, I admit :-)

Now I understand what you want to do, and it's basically
impossible: to figure how long a line is, perl has to read
it before hand. This would be possible, however, with some
Unicode text format where lines are prefixed with their
length (if I remember properly); in that case you could
write a program that would seek to the next line.

--
perl -e 'print `echo Just a Lame Perl Luser | gzip -9 | cat | gzip -cd`'



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

This Article:

Quote:
> > > Is it possible to make the <> ignore lines which contain more than
> > > an arbitrary amount of characters when reading a file (or any other
> > > source of data)?
> Obviously that's not what I'm looking for. I want to entirely skip lines
> which are too long to avoid my program chocking if someone tries to pass
> it 100MB in one line (extreme example, I admit :-)

Different Article:

Quote:
> On the next 'read' you will not get the beginning of the next line,
> you'll get the rest of the line ...
> > > That's right. And that's a problem I ran into when toying with 'read'
> > > just now. Is there any way to read only n-bytes of each line read by <>?

(Parameters Changed In Other Articles)

You have changed your parameters a number of times
within this thread and parallel threads. Do you
know what you want to do? What you want to do,
what method is used, makes a difference.

Consider making a decision as to what you want
to do and, stick with this decision. This will
help you to avoid annoying those who would help
you, by your constantly changing your parameters.

Constantly changing your parameters, asking a
question and responding with argument,
smacks of trolling.

Godzilla!
--
Dr. Kiralynne Schilitubi | Cooling Fan Specialist
UofD: University of Duh! | ENIAC Hard Wiring Pro
BumScrew, South of Egypt | HTML Programming Class

TEST SCRIPT:
____________

#!/usr/local/bin/perl

print "Content-type: text/plain\n\n";

print "Input:\n\n";

open (TEST, "test.txt");

while (<TEST>)
 { print $_; }

close (TEST);

print "\n\nLength Method:\n\n";

open (TEST, "test.txt");

while (<TEST>)
 {
  if (length ($_) <= 17)
   { print $_; }
 }

close (TEST);

print "\n\nSubstring Method:\n\n";

open (TEST, "test.txt");

while (<TEST>)
 {
  $_ = substr ($_, 0, 16);
  print "$_\n";
 }

close (TEST);

exit;

PRINTED RESULTS:
________________

Input:

read this line 1 no
read this line 2
read this line 3 no
read this line 4
read this line 5 no

Length Method:

read this line 2
read this line 4

Substring Method:

read this line 1
read this line 2
read this line 3
read this line 4
read this line 5



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:

>Hi,

>        Is it possible to make the <> ignore lines which contain more than
>an arbitrary amount of characters when reading a file (or any other
>source of data)?

>Regards,
>Bernard

From the other responses in this thread I'd guess you can't do this
with <>. I couldn't tell if you wanted to prevent wasting storage
space or time. If you are willing to take the time, you can read only
the length you are willing to store and check to see if it contains a
newline character. If it does, store it then read the next piece.
This may give you some ideas.

use strict;

my $test;
my $size = -s 'file';
my $length = 100;
open (FILE, 'file') or die "$!";
while (tell FILE < $size) {
    read(FILE, $test, $length);
    print $test unless (index $test, "\n") == -1;
    }
close FILE;

--

I probably won't read anything sent there. |



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:



> >> Hi,

> >>         Is it possible to make the <> ignore lines which contain more than
> >> an arbitrary amount of characters when reading a file (or any other
> >> source of data)?

> >> Regards,
> >> Bernard
> >> --
> >> perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
> >> print split/No such file or directory/;'

> >my $MAX=1024; # kbyte

> >while (<>) {

> >  next if length > $MAX;

> >  # otherwise process the line

> >}

> Obviously that's not what I'm looking for. I want to entirely skip lines
> which are too long to avoid my program chocking if someone tries to pass
> it 100MB in one line (extreme example, I admit :-)

> Cheers,
> Bernard
> --
> perl -le 'open JustAnotherPerlHacker,""or$_="B$!e$!r$!n$!a$!r$!d$!";
> print split/No such file or directory/;'

fine.  then "read" one byte at a time appending each into a string until
you hit your max and blow away the string and read and dump bytes until
you hit an newline.


Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:


> to say:

> > Obviously that's not what I'm looking for. I want to entirely skip lines
> > which are too long to avoid my program chocking if someone tries to pass
> > it 100MB in one line (extreme example, I admit :-)

> Now I understand what you want to do, and it's basically
> impossible: to figure how long a line is, perl has to read
> it before hand. This would be possible, however, with some
> Unicode text format where lines are prefixed with their
> length (if I remember properly); in that case you could
> write a program that would seek to the next line.

While it is true that you will have to read the entire line to know
how long it is (and to get to the next line, for that matter), that
doesn't mean that you have to read it all at once.  Fall back on a
more C-like solution, and read the file in preset chunks that are
small enough to handle.  Within each chunk, search for the line
terminator (with index).  If the line limit is exceeded, you still
keep reading chunks and searching for the line terminator, but you
don't save any of the data.  Then when you find the line terminator,
you start over for the next line.

Perl-ish Pseudo-code:

while ($chunk .= read a chunk) {
  while (chunk not empty) {
    if (line terminator) {
      if (flag set) {
        throw away line from chunk
        unset flag
      } else {
        extract line from chunk and process line
      }
    } else {
      if (size of chunk > max line size) {
        throw away chunk
        set flag to ignore this line
      }
    }
  }

Quote:
}

--
Ren Maddox



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>
:       Is it possible to make the <> ignore lines which contain more than
: an arbitrary amount of characters when reading a file (or any other
: source of data)?

It's not really feasible to make <> ignore the lines, but your code can do
so trivially:

  while (<SOME_DATA_SOURCE>) {
    next if length > 255;   # ...or whatever limit you want
    # Short-line processing goes here
  }

Alternatively, if you're slurping the whole file with <>, you can
post-process away the long lines with grep:


--
   |   Craig Berry - http://www.cinenet.net/~cberry/
 --*--  "Quidquid latine dictum sit, altum viditur."
   |



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

Quote:


> ' Constantly changing your parameters, asking a
> ' question and responding with argument,
> ' smacks of trolling.
> Sounds to me more like how a client specifies a program usually
> accompanied by a lot of hand waving.

People like this, I am often tempted to give
'em a good smack upside the head. Wish I could,
legally. SMACK! Smarten up you arsehole.

It not my usual habit to whine, however with this
extraordinary amount of fake articles written with
malice intent being posted under myriad fake
header information, all fairly much by the same
person, I become increasingly annoyed with having
my personal time wasted by this idiot.

Fake articles are posted, people sucker into these
fake articles, pointless dialog takes place and,
others besides myself, have our time wasted by being
forced to filter through and wade through what amounts
to a bunch of mule manure. A killfile doesn't work
for this person with being able to fake entire header
information at whim and will via a server. Surprising
how few, if any, have noticed most of these fake email
addresses, included with fake header information, either
don't exist or don't belong to this poser using a server
in an unethical manner, if not perhaps unlawful manner.

Personally, I don't like having someone like this
rob me of my personal time and personal enjoyment
just to get his {*filter*}ed ego rocks off.

Some of you dare to label me a troll?

*laughs*

How ignorantly gullible are some of you.

SMACK! SMACK! Smarten up you arseholes!

hehhh... heh... heh...

Godzilla!
--
Dr. Kiralynne Schilitubi | Cooling Fan Specialist
UofD: University of Duh! | ENIAC Hard Wiring Pro
BumScrew, South of Egypt | HTML Programming Class



Wed, 18 Jun 1902 08:00:00 GMT  
 Controlling line length read by <>

(snippage)

Oh yes, have you noticed how often this poser's articles
are double posted?

This boy is so lacking in technological know-how, he
screws up, a lot. Last major{*filter*}up resulted in
more than twenty-thousand articles being duplicated
and posted, articles of other people, all posted to
newsgroups dealing with computer languages, and only
newsgroups dealing with computer languages. Odd that.

*maniacal laughter*

Couple of years back, this cretin offered me a position
as systems operator for his server, knowing of my skills,
a position which I declined, knowing of him.

Ironically, he needs someone like me to straighten out
these{*filter*}ups of his, which extend far beyond
his peer news feed.

Why do you think he is here asking for help but concealing
his need for technological help, behind fake articles?

Suppose I would keep my mouth shut. However, his constant
harassment of me via faked articles, his rumor mongering
and secretive email gossip, pisses me off.

'tant pour tant'

*smiles*

Godzilla!
--
Dr. Kiralynne Schilitubi | Cooling Fan Specialist
UofD: University of Duh! | ENIAC Hard Wiring Pro
BumScrew, South of Egypt | HTML Programming Class



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. maximum command line length <tcsh>

2. <<<<<< HELP >>>>>>>>

3. Limiting maximum line length during line based read

4. >>>> A Singaporean Girl looking for more penpals!!<<<<<

5. control characters in <TEXTAREA> boxes

6. <<>> COMPUTER SOFTWARE / HARDWARE <<>>

7. - - - = = =<><> W e b - U S A <><>= = = - - -

8. <<>> COMPUTER SOFTWARE / HARDWARE <<>>

9. <> for binary file, line length, unpack,read

10. read varilble length lines

11. ==>>COMPUTER HARDWARE / SOFTWARE <<==

 

 
Powered by phpBB® Forum Software