[19783] in Perl-Users-Digest
Perl-Users Digest, Issue: 1978 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 22 06:10:25 2001
Date: Mon, 22 Oct 2001 03:10:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1003745409-v10-i1978@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Mon, 22 Oct 2001 Volume: 10 Number: 1978
Today's topics:
Reading two variables from a file <peter_laranc@yahoo.com>
Re: Reading two variables from a file (Mark Jason Dominus)
Re: Reading two variables from a file <Thomas@Baetzler.de>
Re: Reading two variables from a file <peter_laranc@yahoo.com>
Re: Reading two variables from a file (Anno Siegel)
Re: Reading two variables from a file <please@no.spam>
Re: Remove duplicates from a logfile <please@no.spam>
Re: requiring packages dynamically (Mark Jason Dominus)
Re: Sending commands (keyboard inputs) from a Perl-scri <goldbb2@earthlink.net>
Re: traversing directories <goldbb2@earthlink.net>
Re: xsub - does anything work? <goldbb2@earthlink.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 22 Oct 2001 09:33:31 +0200
From: "Peter" <peter_laranc@yahoo.com>
Subject: Reading two variables from a file
Message-Id: <9r0gtl$i0h$1@newstoo.ericsson.se>
Hi
I have a small program reading the IP address and hostname from a file and
save these two information in to some variables. My problem is that I don't
know how could I separate IP and hostname from each other.
My input file has following format:
# Line with # should be ignored
192.168.0.1 PC1234
192.168.0.2 PC1235
-----
My program could only reads the IP but not able to read the host name i dont
know how could i correct it.
Here is my program is
open (FILE,'device.cfg') or die "Cant open device.cfg $! ";
my $data = <FILE>;
while(defined (my $data =<FILE>)){
# I should discard line with # and only reads a line having IP and hostname.
if ($line =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/) {
print "The IP is : ",$1," and the host name is : ",$2," \n";
}
else {print "The input data did not contains IP and hostname \n"; }
$data = <FILE>;
}
Thanks in advance Peter
------------------------------
Date: Mon, 22 Oct 2001 07:51:45 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Reading two variables from a file
Message-Id: <3bd3d010.27d0$1b1@news.op.net>
In article <9r0gtl$i0h$1@newstoo.ericsson.se>,
Peter <peter_laranc@yahoo.com> wrote:
>while(defined (my $data =<FILE>)){
>
># I should discard line with # and only reads a line having IP and hostname.
next if $data =~ /^#/; # skip lines that begin with #
my ($ip, $host) = split;
if (defined $ip) {
print "The IP is : ",$ip," and the host name is : ",$host," \n";
} else {
print "The input data did not contains IP and hostname \n";
}
>
>}
Isn't that easy?
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Mon, 22 Oct 2001 10:08:32 +0200
From: =?ISO-8859-1?Q?Thomas_B=E4tzler?= <Thomas@Baetzler.de>
Subject: Re: Reading two variables from a file
Message-Id: <5ik7tt8sq0lebm7i7p52se9io7cutuutav@4ax.com>
On Mon, 22 Oct 2001, "Peter" <peter_laranc@yahoo.com> wrote:
>I have a small program reading the IP address and hostname from a file and
>save these two information in to some variables. My problem is that I don't
>know how could I separate IP and hostname from each other.
>
>My input file has following format:
>
># Line with # should be ignored
>
>192.168.0.1 PC1234
>
>192.168.0.2 PC1235
#!/usr/bin/perl -w
use strict;
>open (FILE,'device.cfg') or die "Cant open device.cfg $! ";
>
>my $data = <FILE>;
don't do that - this will gobble up the first line of your file.
>while(defined (my $data =<FILE>)){
# get rid of leading/trailing whitespace
$data =~ s/^\s*(.*?)\s*/$1/;
># I should discard line with # and only reads a line having IP and hostname.
# skip over comments and empty lines
next if $data =~ m/^#/ or $data eq "";
my( $ip, $hostname ) = split /\s+/, $data;
# alternatively with checking for a valid IP:
if( my($ip, $hostname ) = ($data =~m/(\d+\.\d+\.\d+\.\d+)\s+(.*?)/) ){
#>if ($line =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/) {
# \s matches whitespace, \S non-whitespace - you messed this up
print "The IP is : ",$1," and the host name is : ",$2," \n";
} else {
print "The input data did not contains IP and hostname \n";
}
#> $data = <FILE>;
# don't do that - it happens in the while() head.
}
HTH,
--
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl
------------------------------
Date: Mon, 22 Oct 2001 10:22:56 +0200
From: "Peter" <peter_laranc@yahoo.com>
Subject: Re: Reading two variables from a file
Message-Id: <9r0jq9$gpg$1@newstoo.ericsson.se>
Hi Mark.
Thanks for your answer, but it dident worked. Maybe i dident explained well.
Because each time i read one line from the file, this lines could contains
IP address and hostname, i want to save IP address in one variable and host
name in other.
thats way i have wrote
if ($data =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/)
Problem is that, there is some thing wrong with my if condition, so it only
tacking care of IP address, that means i can only write my $Ip = $1, But i
dont know what should i add or change to get the hostname in $2 or other $.
My code is here:
open (FILE,'device.cfg') or die "Cant open device.cfg $! ";
my $data = <FILE>;
while(defined (my $data =<FILE>)){
# I should discard line with # and only reads a line having IP and hostname.
if ($data =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/) {
# ?????? !!!!! This if condition could be wrong thats way i only getts IP
address, it does not have hostname in $2
print "The IP is : ",$1," and the host name is : ",$2," \n";
# i am not getting host name in $2 i dont know why, or what should i add in
if sentense ?????
}
else {print "The input data did not contains IP and hostname \n"; }
$data = <FILE>;
}
==========
device.cfg containes data like:
# here is a demo for data in device.cfg
192.168.0.1 PC1234
192.168.0.2 PC1235
192.168.0.3 PC1236
------------------------------
Date: 22 Oct 2001 09:02:18 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Reading two variables from a file
Message-Id: <9r0naq$8qu$1@mamenchi.zrz.TU-Berlin.DE>
According to Peter <peter_laranc@yahoo.com>:
> Hi
> I have a small program reading the IP address and hostname from a file and
> save these two information in to some variables. My problem is that I don't
> know how could I separate IP and hostname from each other.
>
> My input file has following format:
>
> # Line with # should be ignored
>
> 192.168.0.1 PC1234
>
> 192.168.0.2 PC1235
>
> -----
>
> My program could only reads the IP but not able to read the host name i dont
> know how could i correct it.
>
> Here is my program is
>
> open (FILE,'device.cfg') or die "Cant open device.cfg $! ";
>
> my $data = <FILE>;
You are reading a single line here, presumably to skip the comment
line. That should be done inside the loop.
> while(defined (my $data =<FILE>)){
>
> # I should discard line with # and only reads a line having IP and hostname.
So why don't you:
next if $data =~ /^#/; # or similar to skip comments
> if ($line =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/) {
Well, first of all, you are checking $line, but the input is in $data.
It looks like you didn't cut-and-paste your program but retyped it.
Please don't do that.
Moreover, you swapped \S and \s in the final part. If you change
them,
/^((\d+)\S(\d+)\S(\d+)\S(\d+))\s+(\S*)$/)
actually works, but is a little imprecise in matching any quadruple
of numbers separated by non-spaces.
/((\d+)\.(\d+)\.(\d+)\.(\d+))\s+(\S+)/
only matches dot-separated quadruples.
> print "The IP is : ",$1," and the host name is : ",$2," \n";
You are looking for the host name in the wrong place. The capturing
variables $1, $2, etc are numbered like the opening parentheses to
which they belong, so $2 not the host name but the first number
out of the IP-Address. The host name is in $6.
> }
> else {print "The input data did not contains IP and hostname \n"; }
> $data = <FILE>;
>
>
> }
Anno
------------------------------
Date: Mon, 22 Oct 2001 09:16:21 GMT
From: Andrew Cady <please@no.spam>
Subject: Re: Reading two variables from a file
Message-Id: <87n12k6nqi.fsf@homer.cghm>
"Peter" <peter_laranc@yahoo.com> writes:
> Hi Mark.
>
> Thanks for your answer, but it dident worked. Maybe i dident
> explained well. Because each time i read one line from the file,
> this lines could contains IP address and hostname, i want to save IP
> address in one variable and host name in other. thats way i have
> wrote
>
> if ($data =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/)
>
> Problem is that, there is some thing wrong with my if condition, so
> it only tacking care of IP address, that means i can only write my
> $Ip = $1, But i dont know what should i add or change to get the
> hostname in $2 or other $.
The problem is the regex. For one thing, $2 matches the text in the
second matching parens, whether or not they're nested. Here's your
regex:
> if ($data =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/)
...................^^^^^
and here are the second matching parens. What you want are these
ones:
> if ($data =~ m/^((\d+)\S(\d+)\S(\d+)\S(\d+))\S+(\s*)$/)
.................................................^^^^^
which right now are 6th; they're put into $6.
But why are you using all those parens anyway? What are you doing
with them? They don't do anything within the match, they just capture
variables in $2, $3, $4, and $5.
Also, you are evidently quite confused about the meaning of \s and \S.
Your last parens match zero or more characters of whitespace followed
by the end of the line. Surely you do not want to match whitespace?
I'll give you a better regex but do you really need to try and verify
that the IP contains 4 fields of digits seperated by periods?
Anyway, if you insist on a regex, use this one:
/^\s*(\d+\.\d+\.\d+\.\d+)\s+(\S+)/
That puts the IP $1 and the next word on the line in $2. Of course,
it will not realize something like this cannot be an IP:
300.400.12341234123413241324.9999999999999999999999999999
> My code is here:
>
> open (FILE,'device.cfg') or die "Cant open device.cfg $! ";
>
> my $data = <FILE>;
Why are you discarding the first line?
> while(defined (my $data =<FILE>)){
You're masking the global $data with a $data in the scope of while.
This is very bad. I assume you're not doing it on purpose. Even if
you used the same $data, you'd be overwriting it before using it the
first time. Don't read it the first time. It gets read before the
block of the while loop executes.
[...]
> $data = <FILE>;
Why are you doing this again?
------------------------------
Date: Mon, 22 Oct 2001 09:36:30 GMT
From: Andrew Cady <please@no.spam>
Subject: Re: Remove duplicates from a logfile
Message-Id: <87adyk6msx.fsf@homer.cghm>
garry@ifr.zvolve.net (Garry Williams) writes:
>
> Yes, if I understand that you simply want to print the first
> occurrence of each unique IP address.
>
> Here's a way:
>
> $ perl -ne 'print unless $seen{ (split)[4] }++' log_file
>
> Hope this helps.
That prints the whole line, not just the IP address.
perl -lne 'print unless $seen{ $_ = (split)[4] }++'
------------------------------
Date: Mon, 22 Oct 2001 08:06:17 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: requiring packages dynamically
Message-Id: <3bd3d378.2847$62@news.op.net>
In article <Pine.LNX.4.10.10110201355140.7528-100000@gloria.cam.org>,
Marc Tardif <intmktg@gloria.cam.org> wrote:
>Is there a portable way to require a package after
>building its name dynamically?
>foreach (qw(Foo Bar)) {
> my $package = "One::Two::$_";
> require $package;
>}
Probably the
eval "require $package";
suggestion in the manual is the best strategy. But you may want to consider:
use File::Spec::Functions 'catfile';
foreach (qw(Foo Bar)) {
my $package = "One::Two::$_";
$file = catfile(split /::/, $package);
$file .= ".pm";
require $file;
}
catfile() will assemble the path components in an OS-portable way.
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Mon, 22 Oct 2001 03:09:36 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Sending commands (keyboard inputs) from a Perl-script to another process in linux?
Message-Id: <3BD3C630.A093C2B@earthlink.net>
Emil wrote:
>
> Hi!
> I was wondering if it's possible for a perl-script to send commands to
> another process in linux? Like starting the process, then remote
> controll it with keyboard inputs and then get the result?
If it really and truly wants a keyboard, then you need a pty of some
sort... one of IPC::Run, Expect, or IO::Pty might do what you want.
If all you need to do is send to it's standard input and read from it's
standard output, then IPC::Open2 is probably your thing.
--
"What does stupid old man mean pidgin talk?
Shampoo does not talk like a bird."
------------------------------
Date: Mon, 22 Oct 2001 03:31:29 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: traversing directories
Message-Id: <3BD3CB51.E411A784@earthlink.net>
Ashley M. Kirchner wrote:
>
> I have the following script to play MP3s on my server:
>
> #!/usr/bin/perl
>
> srand;
> for (;;)
> {
> @f = <*.mp3>;
> @s=();
> (push(@s,splice(@f,rand @f,1))) while (@f);
> map {system ("/usr/freeware/bin/amp","-p",$_) && exit;} @s;
> }
>
> However, I'm going to start splitting up my collections into
> subdirectories, ala:
>
> /mp3s/<artist>/<album>/<songs>
>
> How can I have that same script, sitting in the root /mp3s
> directory, be able to traverse the whole tree and do the same thing
> it's doing now (which is randomly pick a song and play it)?
There's a few ways: The simplest would be to take advantage of the
directory structure which you've described, and do this:
#!/usr/bin/perl -w
use strict; srand;
my @mp3s = glob("/mp3s/*/*/*.mp3");
for( my $i = $#mp3s; $i >= 0; --$i ) {
my ($mp3) = splice(@mp3s,rand(@mp3s),1);
system("/usr/freeware/bin/amp", "-p", $mp3)
and exit;
}
__END__
If you don't have them all in this structure, then you need to do a
recursive search:
#!/usr/bin/perl -w
use strict; use File::Find; srand; my @mp3s;
find( sub { push @mp3s, $File::Find::name if /\.mp3\z/ }, "/mp3s" );
for( my $i = $#mp3s; $i >= 0; --$i ) {
my ($mp3) = splice(@mp3s,rand(@mp3s),1);
system("/usr/freeware/bin/amp", "-p", $mp3)
and exit;
}
__END__
--
"What does stupid old man mean pidgin talk?
Shampoo does not talk like a bird."
------------------------------
Date: Mon, 22 Oct 2001 03:16:32 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: xsub - does anything work?
Message-Id: <3BD3C7D0.7E70BD8D@earthlink.net>
Richard Trahan wrote:
>
> Has anyone gotten anything at all, however simple, to even compile
> on Windoze, using the instructions in perlxstut or perlxs?
>
> And while I'm at it, has anyone gotten anything at all, however
> simple, to even compile on Windoze, with SWIG?
[snip]
> More generally, is there any software, anywhere, of any type, written
> by anyone for any purpose, that actually works? Please point me to it
> so I can study it for my edification.
This is only vaguely related to XS or SWIG, but have you considered
writing your code with Inline::C ? Surely that would be a much better
module for a beginner to use.
--
"What does stupid old man mean pidgin talk?
Shampoo does not talk like a bird."
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 1978
***************************************