Searching in a line

A

A. Sinan Unur

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.

Not good, though.

use strict;

missing.

More importantly, this version still slurps the entire file.

while ( <FILE> ) {
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
 
X

xhoster

Jürgen Exner said:
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
 
T

Tad McClellan

Petr Vileta said:
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
 
J

Jürgen Exner

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
 
B

Benoit Lefebvre

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 = `cat bleh` for basic scripts

At least someone gave him a working example ;-)

--Ben
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top