Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: grep in line and print

by justbow (Initiate)
on Jan 24, 2013 at 04:41 UTC ( [id://1015072]=note: print w/replies, xml ) Need Help??


in reply to Re: grep in line and print
in thread grep in line and print

Hello Athanasius, Thanks for the suggestion, But im still getting the error :D I'm trying the code :
#! perl use strict; use warnings; $allmac = "~/temp/allmac.txt"; print $allmac; while (my $line = $allmac) { if ($line =~ /C4:46:19:75:C1:55/) { $id = (split /\s+/, $line)[0]; print "ID = $id\n"; } }
And getting error :
Global symbol "$allmac" requires explicit package name at ./filtermac. +pl line 5. Global symbol "$allmac" requires explicit package name at ./filtermac. +pl line 6. Global symbol "$allmac" requires explicit package name at ./filtermac. +pl line 7. Global symbol "$id" requires explicit package name at ./filtermac.pl l +ine 11. Global symbol "$id" requires explicit package name at ./filtermac.pl l +ine 12. Execution of ./filtermac.pl aborted due to compilation errors.

Replies are listed 'Best First'.
Re^3: grep in line and print
by Athanasius (Archbishop) on Jan 24, 2013 at 05:47 UTC

    With use strict in effect, you need to declare your variable names:

    my $allmac = ... ... my $id = ...

    But the line:

    while (my $line = $allmac)

    won’t work: it’s assigning the file name to $line, but it needs to call readline on a file handle, like so:

    open (my $fh, '<', $allmac) or die "Can't open file '$allmac' for read +ing: $!"; while (my $line = <$fh>) { ....

    Applying the diamond operator to a filehandle: <$fh> is the standard way to call readline in Perl. See readline and I/O Operators; also open and perlopentut.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1015072]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found