Searching in a line

L

lerameur

lerameur said:
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

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
 
M

Mumia W.

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?
 
L

lerameur

lerameur said:
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.
Put neither of those is accepting syntax errors, either.

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
 
L

lerameur

lerameur said:
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.
Put neither of those is accepting syntax errors, either.

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
 
U

Uri Guttman

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
 
J

Jürgen Exner

lerameur said:
lerameur said:
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
 
L

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;
 
L

lerameur

did not see the second page,

Added the strict and warnings.

I do not know how the multiple post got there.

k
 
J

Jürgen Exner

lerameur said:
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
 
L

lerameur

Here you are reading each line into $data


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 ?

k
 
J

Jürgen Exner

lerameur said:
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
 
J

John W. Krahn

lerameur said:
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
 
L

lerameur

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.
I guess I will sue the default one.

k
 
J

Jürgen Exner

lerameur said:
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
 
L

lerameur

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?
Well, I just graduated from school so not that long, I mostly
programmed microcontrollers

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

while (<$fh>) {

Its working great now. thanks
 
B

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";
}
 
B

Benoit Lefebvre

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

#!/usr/bin/perl -w

open (FILE,"<file.txt");

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

close (FILE);
 
J

Jürgen Exner

Benoit said:
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";
}
 
J

Jürgen Exner

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

#!/usr/bin/perl -w

open (FILE,"<file.txt");

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

Much better.

jue
 
L

lerameur

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

open (FILE,"<file.txt");
foreach $line (<FILE>) {
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

k
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,674
Latest member
scazeho

Latest Threads

Top