Searching in a line

Searching in a line

am 29.08.2007 22:45:47 von lerameur

Hello all,

I want to do a search in a log file. each line has the following
pattern:
34,56,,9,0,0,,,,65;
So there 10 section. How do I parse through different sections?
meaning I want section 4, which is the number 9

thanks
ken

Re: Searching in a line

am 29.08.2007 22:51:40 von it_says_BALLS_on_your forehead

On Aug 29, 4:45 pm, lerameur wrote:
> Hello all,
>
> I want to do a search in a log file. each line has the following
> pattern:
> 34,56,,9,0,0,,,,65;
> So there 10 section. How do I parse through different sections?
> meaning I want section 4, which is the number 9

if every possible value within a 'section' cannot contain a comma ',',
then you can just split on a comma, and take the 4th section.

my $log = 'log.txt';
open my $fh, '<', $log or die "can't open '$log': $!\n";
while ( <$fh> ) {
my $val = (split /,/)[3];
print $val, "\n";
}
close $fh;

Re: Searching in a line

am 29.08.2007 23:25:47 von jurgenex

lerameur wrote:
> Hello all,
>
> I want to do a search in a log file. each line has the following
> pattern:
> 34,56,,9,0,0,,,,65;
> So there 10 section. How do I parse through different sections?

perldoc -f split

> meaning I want section 4, which is the number 9

$sec4 = split($value)[3]

jue

Re: Searching in a line

am 29.08.2007 23:37:03 von lerameur

On Aug 29, 5:25 pm, "Jürgen Exner" wrote:
> lerameur wrote:
> > Hello all,
>
> > I want to do a search in a log file. each line has the following
> > pattern:
> > 34,56,,9,0,0,,,,65;
> > So there 10 section. How do I parse through different sections?
>
> perldoc -f split
>
> > meaning I want section 4, which is the number 9
>
> $sec4 =3D split($value)[3]
>
> jue


Hi
either way I am getting these error messages:
../spam2: my: command not found
../spam2: line 6: unexpected EOF while looking for matching `"'
../spam2: line 10: syntax error: unexpected end of file

ken

Re: Searching in a line

am 29.08.2007 23:50:35 von jurgenex

lerameur wrote:
> On Aug 29, 5:25 pm, "Jürgen Exner" wrote:
>> lerameur wrote:
>>> I want to do a search in a log file. each line has the following
>>> pattern:
>>> 34,56,,9,0,0,,,,65;
>>> So there 10 section. How do I parse through different sections?
>>
>> perldoc -f split
>>
>>> meaning I want section 4, which is the number 9
>>
>> $sec4 = split($value)[3]
>
> either way I am getting these error messages:
> ./spam2: my: command not found
> ./spam2: line 6: unexpected EOF while looking for matching `"'
> ./spam2: line 10: syntax error: unexpected end of file

Ok, ok, that split() syntax is not correct, I forgot to specify the pattern.
If you had included a self-contained minimal program then I could have
tested my suggestion and that would not have happened.

As for the other error messages: you didn't show us your program, therefore
there is no way for us to see what is wrong. Have you read the posting
guidelines that are posted here frequently?

jue

Re: Searching in a line

am 30.08.2007 00:00:27 von jurgenex

Jürgen Exner wrote:
> lerameur wrote:
>> On Aug 29, 5:25 pm, "Jürgen Exner" wrote:
>>> lerameur wrote:
>>>> I want to do a search in a log file. each line has the following
>>>> pattern:
>>>> 34,56,,9,0,0,,,,65;
>>>> So there 10 section. How do I parse through different sections?

> Ok, ok, that split() syntax is not correct, I forgot to specify the
> pattern. If you had included a self-contained minimal program then I
> could have tested my suggestion and that would not have happened.

Here's the spoonfed version:

my $foo = '34,56,,9,0,0,,,,65';
my $bar = (split(/,/,$foo))[3];
print $bar;

jue

Re: Searching in a line

am 30.08.2007 06:14:50 von Peter Makholm

lerameur writes:

> Hi
> either way I am getting these error messages:
> ./spam2: my: command not found
> ./spam2: line 6: unexpected EOF while looking for matching `"'
> ./spam2: line 10: syntax error: unexpected end of file

Looks like you're trying to use bash af perl interpreter. That
wouldn't work. Did you forgot the #!-line?

//Makholm

Re: Searching in a line

am 30.08.2007 14:00:25 von lerameur

On Aug 30, 12:14 am, Peter Makholm wrote:
> lerameur writes:
> > Hi
> > either way I am getting these error messages:
> > ./spam2: my: command not found
> > ./spam2: line 6: unexpected EOF while looking for matching `"'
> > ./spam2: line 10: syntax error: unexpected end of file
>
> Looks like you're trying to use bash af perl interpreter. That
> wouldn't work. Did you forgot the #!-line?
>
> //Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic

Re: Searching in a line

am 30.08.2007 14:10:18 von lerameur

On Aug 30, 12:14 am, Peter Makholm wrote:
> lerameur writes:
> > Hi
> > either way I am getting these error messages:
> > ./spam2: my: command not found
> > ./spam2: line 6: unexpected EOF while looking for matching `"'
> > ./spam2: line 10: syntax error: unexpected end of file
>
> Looks like you're trying to use bash af perl interpreter. That
> wouldn't work. Did you forgot the #!-line?
>
> //Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic

Re: Searching in a line

am 30.08.2007 14:25:44 von lerameur

On Aug 30, 12:14 am, Peter Makholm wrote:
> lerameur writes:
> > Hi
> > either way I am getting these error messages:
> > ./spam2: my: command not found
> > ./spam2: line 6: unexpected EOF while looking for matching `"'
> > ./spam2: line 10: syntax error: unexpected end of file
>
> Looks like you're trying to use bash af perl interpreter. That
> wouldn't work. Did you forgot the #!-line?
>
> //Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic

Re: Searching in a line

am 30.08.2007 14:28:11 von jurgenex

lerameur wrote:
> my $log = 'file.txt';
> open my $fh, '<', $log or die "cannot open '$log': $!\n";
> while (<$fh>) {
>
> my $val =(split( /,/,$log))[3]
> print $val, "\n";
>
> }
> close $fh;
>
> getting compilation error.

Well, the indentation gives it away: the my $val=... statement is missing
the trailing semicolon, dude.

> New to perl, different then C and Basic

Put neither of those is accepting syntax errors, either.

jue

Re: Searching in a line

am 30.08.2007 14:38:49 von lerameur

On Aug 30, 12:14 am, Peter Makholm wrote:
> lerameur writes:
> > Hi
> > either way I am getting these error messages:
> > ./spam2: my: command not found
> > ./spam2: line 6: unexpected EOF while looking for matching `"'
> > ./spam2: line 10: syntax error: unexpected end of file
>
> Looks like you're trying to use bash af perl interpreter. That
> wouldn't work. Did you forgot the #!-line?
>
> //Makholm

Thanks fo ryour help

I re-wrote the program like you said without #! line.. Doh

here your/my program: (the file.txt file i sin the same directory with
two lines)
12,45,7,435,435,42342,2432;
243,454,324,45,2,4;
~

the program :

#!/usr/bin/perl

my $log = 'file.txt';
open my $fh, '<', $log or die "cannot open '$log': $!\n";
while (<$fh>) {

my $val =(split( /,/,$log))[3]
print $val, "\n";

}
close $fh;

getting compilation error.
New to perl, different then C and Basic

Re: Searching in a line

am 30.08.2007 14:52:25 von lerameur

On Aug 30, 8:28 am, "Jürgen Exner" wrote:
> lerameur wrote:
> > my $log =3D 'file.txt';
> > open my $fh, '<', $log or die "cannot open '$log': $!\n";
> > while (<$fh>) {
>
> > my $val =3D(split( /,/,$log))[3]
> > print $val, "\n";
>
> > }
> > close $fh;
>
> > getting compilation error.
>
> Well, the indentation gives it away: the my $val=3D... statement is missi=
ng
> the trailing semicolon, dude.
>
> > New to perl, different then C and Basic
>
> Put neither of those is accepting syntax errors, either.
>
> jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k

Re: Searching in a line

am 30.08.2007 14:58:43 von lerameur

On Aug 30, 8:28 am, "Jürgen Exner" wrote:
> lerameur wrote:
> > my $log =3D 'file.txt';
> > open my $fh, '<', $log or die "cannot open '$log': $!\n";
> > while (<$fh>) {
>
> > my $val =3D(split( /,/,$log))[3]
> > print $val, "\n";
>
> > }
> > close $fh;
>
> > getting compilation error.
>
> Well, the indentation gives it away: the my $val=3D... statement is missi=
ng
> the trailing semicolon, dude.
>
> > New to perl, different then C and Basic
>
> Put neither of those is accepting syntax errors, either.
>
> jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k

Re: Searching in a line

am 30.08.2007 15:02:17 von lerameur

On Aug 30, 8:28 am, "Jürgen Exner" wrote:
> lerameur wrote:
> > my $log =3D 'file.txt';
> > open my $fh, '<', $log or die "cannot open '$log': $!\n";
> > while (<$fh>) {
>
> > my $val =3D(split( /,/,$log))[3]
> > print $val, "\n";
>
> > }
> > close $fh;
>
> > getting compilation error.
>
> Well, the indentation gives it away: the my $val=3D... statement is missi=
ng
> the trailing semicolon, dude.
>
> > New to perl, different then C and Basic
>
> Put neither of those is accepting syntax errors, either.
>
> jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k

Re: Searching in a line

am 30.08.2007 15:04:35 von lerameur

On Aug 30, 8:28 am, "Jürgen Exner" wrote:
> lerameur wrote:
> > my $log =3D 'file.txt';
> > open my $fh, '<', $log or die "cannot open '$log': $!\n";
> > while (<$fh>) {
>
> > my $val =3D(split( /,/,$log))[3]
> > print $val, "\n";
>
> > }
> > close $fh;
>
> > getting compilation error.
>
> Well, the indentation gives it away: the my $val=3D... statement is missi=
ng
> the trailing semicolon, dude.
>
> > New to perl, different then C and Basic
>
> Put neither of those is accepting syntax errors, either.
>
> jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k

Re: Searching in a line

am 30.08.2007 15:08:03 von lerameur

On Aug 30, 8:28 am, "Jürgen Exner" wrote:
> lerameur wrote:
> > my $log =3D 'file.txt';
> > open my $fh, '<', $log or die "cannot open '$log': $!\n";
> > while (<$fh>) {
>
> > my $val =3D(split( /,/,$log))[3]
> > print $val, "\n";
>
> > }
> > close $fh;
>
> > getting compilation error.
>
> Well, the indentation gives it away: the my $val=3D... statement is missi=
ng
> the trailing semicolon, dude.
>
> > New to perl, different then C and Basic
>
> Put neither of those is accepting syntax errors, either.
>
> jue

Indeed...
not getting any errors.
BUt there is no output, when I type >./program_name.pl
it skips two lines and returns to the prompt.

k

Re: Searching in a line

am 30.08.2007 16:20:14 von paduille.4061.mumia.w+nospam

On 08/30/2007 08:13 AM, lerameur wrote:
>
> Indeed...
> not getting any errors.
> BUt there is no output, when I type >./program_name.pl
> it skips two lines and returns to the prompt.
>
> k
>

Why are your posts duplicated as much as six times?

Re: Searching in a line

am 30.08.2007 16:28:54 von lerameur

On Aug 30, 9:35 am, lerameur wrote:
> On Aug 30, 8:28 am, "Jürgen Exner" wrote:
>
>
>
> > lerameur wrote:
> > > my $log =3D 'file.txt';
> > > open my $fh, '<', $log or die "cannot open '$log': $!\n";
> > > while (<$fh>) {
>
> > > my $val =3D(split( /,/,$log))[3]
> > > print $val, "\n";
>
> > > }
> > > close $fh;
>
> > > getting compilation error.
>
> > Well, the indentation gives it away: the my $val=3D... statement is mis=
sing
> > the trailing semicolon, dude.
>
> > > New to perl, different then C and Basic
>
> > Put neither of those is accepting syntax errors, either.
>
> > jue
>
> Indeed...
> not getting any errors.
> BUt there is no output, when I type >./program_name.pl
> it skips two lines and returns to the prompt.
>
> k

Ok I got it working by removing the $log in the my val line, also
removing the my's

k

Re: Searching in a line

am 30.08.2007 16:53:48 von lerameur

On Aug 30, 9:35 am, lerameur wrote:
> On Aug 30, 8:28 am, "Jürgen Exner" wrote:
>
>
>
> > lerameur wrote:
> > > my $log =3D 'file.txt';
> > > open my $fh, '<', $log or die "cannot open '$log': $!\n";
> > > while (<$fh>) {
>
> > > my $val =3D(split( /,/,$log))[3]
> > > print $val, "\n";
>
> > > }
> > > close $fh;
>
> > > getting compilation error.
>
> > Well, the indentation gives it away: the my $val=3D... statement is mis=
sing
> > the trailing semicolon, dude.
>
> > > New to perl, different then C and Basic
>
> > Put neither of those is accepting syntax errors, either.
>
> > jue
>
> Indeed...
> not getting any errors.
> BUt there is no output, when I type >./program_name.pl
> it skips two lines and returns to the prompt.
>
> k

Ok I got it working by removing the $log in the my val line, also
removing the my's

k

Re: Searching in a line

am 30.08.2007 16:58:00 von Uri Guttman

>>>>> "l" == lerameur writes:

l> Ok I got it working by removing the $log in the my val line, also
l> removing the my's

removing my's is not a way to fix perl code. this is like disabling
brakes on a car because you don't want two pedals to deal with.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Searching in a line

am 30.08.2007 17:50:41 von jurgenex

lerameur wrote:
> On Aug 30, 8:28 am, "Jürgen Exner" wrote:
>> lerameur wrote:
>>> my $log = 'file.txt';
>>> open my $fh, '<', $log or die "cannot open '$log': $!\n";
>>> while (<$fh>) {
>>
>>> my $val =(split( /,/,$log))[3]
>>> print $val, "\n";

> BUt there is no output, when I type >./program_name.pl
> it skips two lines and returns to the prompt.

That's odd. You should at least have gotten a warning that $val is
undefined. Aren't you using warnings and strict?

Anyway, you are splitting $log which just contains 'file.txt'. Nothing to
split on a comma there. Use $_ instead.

Oh, and BTW: we heard you the first time. No need to flood the NG with a
dozen identical posts.

Oh, and BTW2:
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

jue

Re: Searching in a line

am 30.08.2007 22:27:47 von lerameur

I wrote this now: If it finds the work beth in the line, then is
splits the line. otherwise print an error.
Now there is beth in the file ( i added it) but the program always
skips to the else statement, end prints it twice.
How should I modify the search if (/^beth/) ?

#!/usr/bin/perl

my $log = 'file.txt';

open my $fh, '<', $log or die "cannot open '$log': $!\n";

while ($data=<$fh>) {

if (/beth/) { #matches only if beth is in line
my $val =(split(/,/,$data))[3];
print $val, "\n";
}
else {
print ("Nope, not working...:\n");
}

}

close $fh;

Re: Searching in a line

am 30.08.2007 22:36:48 von lerameur

did not see the second page,

Added the strict and warnings.

I do not know how the multiple post got there.

k

Re: Searching in a line

am 30.08.2007 22:39:03 von jurgenex

lerameur wrote:
> I wrote this now: If it finds the work beth in the line, then is
> splits the line. otherwise print an error.
> Now there is beth in the file ( i added it) but the program always
> skips to the else statement, end prints it twice.
> How should I modify the search if (/^beth/) ?


> while ($data=<$fh>) {

Here you are reading each line into $data

> if (/beth/) { #matches only if beth is in line

and here you are matching /beth/ against the default $_.

Is that what you meant to do?

jue

Re: Searching in a line

am 30.08.2007 22:50:57 von lerameur

On Aug 30, 4:39 pm, "Jürgen Exner" wrote:
> lerameur wrote:
> > I wrote this now: If it finds the work beth in the line, then is
> > splits the line. otherwise print an error.
> > Now there is beth in the file ( i added it) but the program always
> > skips to the else statement, end prints it twice.
> > How should I modify the search if (/^beth/) ?
> > while ($data=3D<$fh>) {
>
> Here you are reading each line into $data
>
> > if (/beth/) { #matches only if beth is in line
>
> and here you are matching /beth/ against the default $_.
>
> Is that what you meant to do?
>
> jue

so you are saying this is no good:
$data=3D<$fh>

should I putting this into an array then ?

k

Re: Searching in a line

am 30.08.2007 22:57:13 von jurgenex

lerameur wrote:
> On Aug 30, 4:39 pm, "Jürgen Exner" wrote:
>> lerameur wrote:
>>> I wrote this now: If it finds the work beth in the line, then is
>>> splits the line. otherwise print an error.
>>> Now there is beth in the file ( i added it) but the program always
>>> skips to the else statement, end prints it twice.
>>> How should I modify the search if (/^beth/) ?
>>> while ($data=<$fh>) {
>>
>> Here you are reading each line into $data
>>
>>> if (/beth/) { #matches only if beth is in line
>>
>> and here you are matching /beth/ against the default $_.
>>
>> Is that what you meant to do?
>>
>> jue
>
> so you are saying this is no good:
> $data=<$fh>

No. I am saying you should make up your mind if you want to use the default
$_ or your own $data. Either is ok, but not both.

> should I putting this into an array then ?

Only if you insist on wasting memory.

jue

Re: Searching in a line

am 30.08.2007 23:00:36 von Dummy

lerameur wrote:
> On Aug 30, 4:39 pm, "Jürgen Exner" wrote:
>> lerameur wrote:
>>> I wrote this now: If it finds the work beth in the line, then is
>>> splits the line. otherwise print an error.
>>> Now there is beth in the file ( i added it) but the program always
>>> skips to the else statement, end prints it twice.
>>> How should I modify the search if (/^beth/) ?
>>> while ($data=<$fh>) {
>> Here you are reading each line into $data
>>
>>> if (/beth/) { #matches only if beth is in line
>> and here you are matching /beth/ against the default $_.
>>
>> Is that what you meant to do?
>>
>> jue
>
> so you are saying this is no good:
> $data=<$fh>
>
> should I putting this into an array then ?

No, that is *not* what he is saying. You should either use $data in both places:

while ( my $data = <$fh> ) {

if ( $data =~ /beth/ ) { #matches only if beth is in line

Or use $_ in both places:

while ( <$fh> ) {

if ( /beth/ ) { #matches only if beth is in line




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: Searching in a line

am 30.08.2007 23:01:29 von lerameur

On Aug 30, 4:57 pm, "Jürgen Exner" wrote:
> lerameur wrote:
> > On Aug 30, 4:39 pm, "Jürgen Exner" wrote:
> >> lerameur wrote:
> >>> I wrote this now: If it finds the work beth in the line, then is
> >>> splits the line. otherwise print an error.
> >>> Now there is beth in the file ( i added it) but the program always
> >>> skips to the else statement, end prints it twice.
> >>> How should I modify the search if (/^beth/) ?
> >>> while ($data=3D<$fh>) {
>
> >> Here you are reading each line into $data
>
> >>> if (/beth/) { #matches only if beth is in line
>
> >> and here you are matching /beth/ against the default $_.
>
> >> Is that what you meant to do?
>
> >> jue
>
> > so you are saying this is no good:
> > $data=3D<$fh>
>
> No. I am saying you should make up your mind if you want to use the defau=
lt
> $_ or your own $data. Either is ok, but not both.

I do not make difference between the two. This is new to me.
I guess I will sue the default one.

k

Re: Searching in a line

am 31.08.2007 02:15:18 von jurgenex

lerameur wrote:
> On Aug 30, 4:57 pm, "Jürgen Exner" wrote:
>> lerameur wrote:
>>> On Aug 30, 4:39 pm, "Jürgen Exner" wrote:
>>>> lerameur wrote:
>>>>> while ($data=<$fh>) {
>>
>>>> Here you are reading each line into $data
>>
>>>>> if (/beth/) { #matches only if beth is in line
>>
>>>> and here you are matching /beth/ against the default $_.
>>
>> No. I am saying you should make up your mind if you want to use the
>> default $_ or your own $data. Either is ok, but not both.
>
> I do not make difference between the two. This is new to me.

Oh come on! Do you really expect if you are setting one variable named $data
another totally unrelated variable named $_ would magically assume the same
value? How long have you been programming?

> I guess I will sue the default one.

Ok, then don't assign the read line to $data:

while (<$fh>) {

jue

Re: Searching in a line

am 31.08.2007 21:06:11 von lerameur

On Aug 30, 8:15 pm, "Jürgen Exner" wrote:
> lerameur wrote:
> > On Aug 30, 4:57 pm, "Jürgen Exner" wrote:
> >> lerameur wrote:
> >>> On Aug 30, 4:39 pm, "Jürgen Exner" wrote:
> >>>> lerameur wrote:
> >>>>> while ($data=3D<$fh>) {
>
> >>>> Here you are reading each line into $data
>
> >>>>> if (/beth/) { #matches only if beth is in line
>
> >>>> and here you are matching /beth/ against the default $_.
>
> >> No. I am saying you should make up your mind if you want to use the
> >> default $_ or your own $data. Either is ok, but not both.
>
> > I do not make difference between the two. This is new to me.
>
> Oh come on! Do you really expect if you are setting one variable named $d=
ata
> another totally unrelated variable named $_ would magically assume the sa=
me
> value? How long have you been programming?
Well, I just graduated from school so not that long, I mostly
programmed microcontrollers


> > I guess I will sue the default one.
>
> Ok, then don't assign the read line to $data:
>
> while (<$fh>) {

Its working great now. thanks

Re: Searching in a line

am 31.08.2007 22:16:52 von Benoit Lefebvre

Here is how I'd do it..

#!/usr/bin/perl
@list = `cat file.txt | grep beth`;
foreach $line (@list) {
@items = split(",",$line);
print $items[3] . "\n";
}

Re: Searching in a line

am 31.08.2007 22:18:48 von Benoit Lefebvre

Can also be made like that (if you don't want to use the shell
functions)

#!/usr/bin/perl -w

open (FILE,"
foreach $line () {
if ($line =~ m/beth/) {
@items = split(",",$line);
print $items[3] . "\n";
}
}

close (FILE);

Re: Searching in a line

am 01.09.2007 02:30:53 von jurgenex

Benoit Lefebvre wrote:
> Here is how I'd do it..
>
> #!/usr/bin/perl
> @list = `cat file.txt | grep beth`;

Useless use of cat
Useless use of dumb external process for grep (Perl has it's own grep; but
even worse it is not even needed in this case)
Waste of memory (no need to create large array when you can process each
line individually)

> foreach $line (@list) {
> @items = split(",",$line);
> print $items[3] . "\n";
> }

Re: Searching in a line

am 01.09.2007 02:31:46 von jurgenex

Benoit Lefebvre wrote:
> Can also be made like that (if you don't want to use the shell
> functions)
>
> #!/usr/bin/perl -w
>
> open (FILE," >
> foreach $line () {
> if ($line =~ m/beth/) {
> @items = split(",",$line);
> print $items[3] . "\n";

Much better.

jue

Re: Searching in a line

am 02.09.2007 01:59:16 von lerameur

On Sep 1, 3:31 am, "Jürgen Exner" wrote:
> Benoit Lefebvre wrote:
> > Can also be made like that (if you don't want to use the shell
> > functions)
>
> > #!/usr/bin/perl-w
>
> > open (FILE," >
> > foreach $line () {
> > if ($line =3D~ m/beth/) {
> > @items =3D split(",",$line);
> > print $items[3] . "\n";
>
> Much better.
>
> jue

I tried using
use strict;
use warnings;

with the above program and it do not work.
can I know why

k

Re: Searching in a line

am 02.09.2007 03:36:16 von 1usa

"Jürgen Exner" wrote in news:Sp2Ci.655$pm2.601
@trndny08:

> Benoit Lefebvre wrote:
>> Can also be made like that (if you don't want to use the shell
>> functions)
>>
>> #!/usr/bin/perl -w
>>
>> open (FILE," >>
>> foreach $line () {
>> if ($line =~ m/beth/) {
>> @items = split(",",$line);
>> print $items[3] . "\n";
>
> Much better.

Not good, though.

use strict;

missing.

More importantly, this version still slurps the entire file.

while ( ) {
if ( /beth/ ) {
print (split /,/)[3], "\n";
}
}

Of course, open should be checked for failure, lexical filehandles and
3-argument open are preferred and omitting $line and @items actually
increases readability.

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines:

Re: Searching in a line

am 02.09.2007 04:26:40 von xhoster

"Jürgen Exner" wrote:
> Benoit Lefebvre wrote:
> > Here is how I'd do it..
> >
> > #!/usr/bin/perl
> > @list = `cat file.txt | grep beth`;
>
> Useless use of cat

It isn't useless. By doing it that way, it becomes trivially easy
change cat into gzcat if you want to. Making things trivially easy
for me in the future is quite useful.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

Re: Searching in a line

am 02.09.2007 04:35:07 von Petr Vileta

lerameur wrote:
> On Sep 1, 3:31 am, "Jürgen Exner" wrote:
>> Benoit Lefebvre wrote:
>>> Can also be made like that (if you don't want to use the shell
>>> functions)
>>
>>> #!/usr/bin/perl-w
>>
>>> open (FILE," >>
>>> foreach $line () {
>>> if ($line =~ m/beth/) {
>>> @items = split(",",$line);
>>> print $items[3] . "\n";
>>
>> Much better.
>>
>> jue
>
> I tried using
> use strict;
> use warnings;
>
> with the above program and it do not work.
> can I know why
>
When you use "use strict" then you must use "my", "our" or "local" variable
declaration.

open (FILE,"
foreach my $line () {
if ($line =~ m/beth/) {
my @items = split(",",$line);
print $items[3] . "\n";


--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: Searching in a line

am 02.09.2007 13:43:18 von Tad McClellan

Petr Vileta wrote:

> When you use "use strict" then you must use "my", "our" or "local" variable
^^^^ ^^^^^
> declaration.


That is wrong in multiple ways.

"local" does not help with strict's checks.

"use vars" does help with strict's checks.

You are NOT required to use any declaration at all under strict,
you can simply use fully-qualified names instead:

$main::foo # instead of $foo


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Searching in a line

am 02.09.2007 13:58:05 von jurgenex

lerameur wrote:
[...]
> I tried using
> use strict;
> use warnings;
>
> with the above program and it do not work.

"It does not work" it the worst possible problem description. Do you also go
to the doctor and just say "It hurts, please make it stop" without
explaining any symptoms or details? I strongly suggest you read
http://www.catb.org/~esr/faqs/smart-questions.html.

Proposal for a better question:

==========
When I'm adding
use strict;
use warnings;
to the above program then I am getting these error messages:
Global symbol "$line" requires explicit package name at C:\tmp\t.pl line
6.
Global symbol "$line" requires explicit package name at C:\tmp\t.pl line
7.
Global symbol "@items" requires explicit package name at C:\tmp\t.pl
line 8.
[and several more like this]
I checked perldoc and Google, but couldn't find an explanation. What do I
need to do to make the program strict and warnings compliant?

-OR-

I checked perldoc and Google, but I still don't understand these error
messages, could someone please explain?

-OR-

I checked perldoc and found this explanation
[Quote of text]
but I don't understand it. Could someone please explain?

-OR-
I don't understand these error messages and don't know where to start
looking for an explanation, could someone please post a pointer?

===========

Do you see the difference between your posting and the my suggestions above?


Then people would have pointed you to "perldoc perldiag" which has
explanations of all perl messages where it says

Global symbol "%s" requires explicit package name
(F) You've said "use strict vars", which indicates that all
variables must either be lexically scoped (using "my"), declared
beforehand using "our", or explicitly qualified to say which package
the global variable is in (using "::").

or to "perldoc strict" where it says

"strict vars"
This generates a compile-time error if you access a variable that
wasn't declared via "our" or "use vars", localized via "my()", or
wasn't fully qualified. [...] See the my entry in the perlfunc
manpage and the local entry in the perlfunc manpage.

use strict 'vars';
$X::foo = 1; # ok, fully qualified
my $foo = 10; # ok, my() var

jue

Re: Searching in a line

am 02.09.2007 17:48:29 von RedGrittyBrick

xhoster@gmail.com wrote:
> "Jürgen Exner" wrote:
>> Benoit Lefebvre wrote:
>>> Here is how I'd do it..
>>>
>>> #!/usr/bin/perl
>>> @list = `cat file.txt | grep beth`;
>> Useless use of cat
>
> It isn't useless. By doing it that way, it becomes trivially easy
> change cat into gzcat if you want to. Making things trivially easy
> for me in the future is quite useful.
>

If you start with
`cat file.txt | sed -e'' | awk '' | perl -p -e '' | tr a a |grep beth`
it becomes trivially easy to insert a sed/awk/perl/tr command if you
want to.

I'm not sure I like the direction you're headed. :-)

Re: Searching in a line

am 05.09.2007 17:17:52 von Benoit Lefebvre

On Sep 2, 11:48 am, RedGrittyBrick
wrote:
> xhos...@gmail.com wrote:
> > "Jürgen Exner" wrote:
> >>BenoitLefebvrewrote:
> >>> Here is how I'd do it..
>
> >>> #!/usr/bin/perl
> >>> @list =3D `cat file.txt | grep beth`;
> >> Useless use of cat
>
> > It isn't useless. By doing it that way, it becomes trivially easy
> > change cat into gzcat if you want to. Making things trivially easy
> > for me in the future is quite useful.
>
> If you start with
> `cat file.txt | sed -e'' | awk '' | perl -p -e '' | tr a a |grep beth`
> it becomes trivially easy to insert a sed/awk/perl/tr command if you
> want to.
>
> I'm not sure I like the direction you're headed. :-)

Well.. do it however you like it..

I personally use a lot of @var =3D `cat bleh` for basic scripts

At least someone gave him a working example ;-)

--Ben