[31591] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2850 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 4 14:14:39 2010

Date: Thu, 4 Mar 2010 11:14:27 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 4 Mar 2010     Volume: 11 Number: 2850

Today's topics:
        Prevent script from adding an extra line at the end of  <dpich@polartel.com>
    Re: Prevent script from adding an extra line at the end <cartercc@gmail.com>
    Re: Prevent script from adding an extra line at the end <dpich@polartel.com>
    Re: Prevent script from adding an extra line at the end <cartercc@gmail.com>
    Re: Prevent script from adding an extra line at the end <jurgenex@hotmail.com>
    Re: Prevent script from adding an extra line at the end <tadmc@seesig.invalid>
    Re: Prevent script from adding an extra line at the end <dpich@polartel.com>
    Re: Prevent script from adding an extra line at the end <john@castleamber.com>
    Re: Prevent script from adding an extra line at the end <dpich@polartel.com>
    Re: Prevent script from adding an extra line at the end sln@netherlands.com
    Re: Prevent script from adding an extra line at the end <jurgenex@hotmail.com>
    Re: Prevent script from adding an extra line at the end <jurgenex@hotmail.com>
    Re: Prevent script from adding an extra line at the end <cartercc@gmail.com>
    Re: Prevent script from adding an extra line at the end <jurgenex@hotmail.com>
    Re: Prevent script from adding an extra line at the end <dpich@polartel.com>
    Re: Prevent script from adding an extra line at the end <dpich@polartel.com>
    Re: Prevent script from adding an extra line at the end <willem@snail.stack.nl>
        regex match on nothing <john1949@yahoo.com>
    Re: regex match on nothing <josef.moellers@ts.fujitsu.com>
    Re: regex match on nothing <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 04 Mar 2010 10:17:04 -0600
From: Don Pich <dpich@polartel.com>
Subject: Prevent script from adding an extra line at the end of file
Message-Id: <So2dnSBLTeCdQBLWnZ2dnUVZ_hadnZ2d@polarcomm.com>

Here is my script:

#!/usr/bin/perl
use strict;
use warnings;
my $infile = 'Pannaway.txt';
my $outfile = 'BASIP.list';
my $line = '';
open( INFILE, "< $infile" ) or die "Can't open $infile : $!";
open( OUTFILE, "> $outfile" ) or die "Can't open $outfile : $!";
while ($line = <INFILE>){
  if ($line =~ /BAS-/)
    {
      my $IP = '';
      my $Address = '';
      my $Name = '';
      my $mac = '';
      my $type = '';
      my $J6 = '';
      my $J7 = '';
      my $J8 = '';
      my $J9 = '';
      my $J10 = '';
      my $J11 = '';
      my $J12 = '';
      my $J13 = '';
      my $J14 = '';
      my $J15 = '';
      my $J16 = '';
      ($IP,$Address,$Name,$mac,$type,$J6,$J7,$J8,$J9,$J10,$J11,$J12,$J13,
$J14,$J15,$J16) = split '\t',$line;
      my @values = split('\.', $IP);
      foreach my $val (@values) {
	$val = sprintf("%02x", $val);
	printf OUTFILE $val;
      }
      print OUTFILE "\n";
    }
}
close (INFILE);
close (OUTFILE);

Here is the input file 'Pannaway.txt':

10.20.151.250	STTM.15.01	BAS-ADSL48R	00:0A:9F:40:67:05	
3.2.1.28							
10.20.151.248	STTM.16.01	BAS-ADSL48R	00:0A:9F:40:6D:19	
3.2.1.28							
10.20.145.252	STTM.41.01 CO	BAS-ADSL48R	00:0A:9F:40:67:15	
3.2.1.28							
10.20.145.250	STTM.41.02 CO	BAS-ADSL48R	00:0A:9F:00:E5:72	
3.2.1.28							
10.20.128.29	STTM.BAR.01	BAR-GE12	00:0A:9F:50:0D:38	
02.02.00.22							
10.20.128.22	STTM.BAR.02	BAR-GE12	00:0A:9F:50:5E:E9	
02.02.00.22							

Here is the output file BASIP.list:
0a1497fa
0a1497f8
0a1491fc
0a1491fa

**  Notice the extra line after 0a1491fa

I realize that after the field "IP" is converted to hex, it gets out of 
the "foreach" loop and then prints the carriage return to start the next 
line in the file.  Can I redo this script to not have it create that last 
carriage return?  Can I place it in another spot so that it doesn't 
create the extra blank line?


------------------------------

Date: Thu, 4 Mar 2010 08:33:31 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <b775bf50-c424-4677-842c-e465a26f205c@f8g2000yqn.googlegroups.com>

On Mar 4, 11:17=A0am, Don Pich <dp...@polartel.com> wrote:
> I realize that after the field "IP" is converted to hex, it gets out of
> the "foreach" loop and then prints the carriage return to start the next
> line in the file. =A0Can I redo this script to not have it create that la=
st
> carriage return? =A0Can I place it in another spot so that it doesn't
> create the extra blank line?

Print the newline before each value.

Don't forget that you can put your logic in a function and then call
the function. This way, you can do a priming read and write, and put
the newline first in the loop, like this:

data_func();
while (DATA)
{
  print "\n";
  data_func();
}

CC



------------------------------

Date: Thu, 04 Mar 2010 10:36:44 -0600
From: Don Pich <dpich@polartel.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <So2dnSNLTeABfBLWnZ2dnUVZ_hYAAAAA@polarcomm.com>

On Thu, 04 Mar 2010 08:33:31 -0800, ccc31807 wrote:

But won't putting the print line at the beginning give me an empty line 
at the top?


------------------------------

Date: Thu, 4 Mar 2010 08:58:50 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <c0cbbf3e-24b1-4e64-86a6-8f7ae12577ff@z11g2000yqz.googlegroups.com>

On Mar 4, 11:36=A0am, Don Pich <dp...@polartel.com> wrote:
> On Thu, 04 Mar 2010 08:33:31 -0800, ccc31807 wrote:
>
> But won't putting the print line at the beginning give me an empty line
> at the top?

That's the purpose of the priming read (or write). Put your logic in
the function except for the new line. Call the function before the
loop, then enter the loop. Inside the loop, print the new line and
then call the function, like this:

function_call()
while {condition remains true)
{
  print new line;
  function call();
}

CC.


------------------------------

Date: Thu, 04 Mar 2010 09:04:54 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <3jovo5tel2nnj59meftn8pr6fg640vl7tg@4ax.com>

Don Pich <dpich@polartel.com> wrote:
>Here is my script:
>
>#!/usr/bin/perl
>use strict;
>use warnings;
>my $infile = 'Pannaway.txt';
>my $outfile = 'BASIP.list';
>my $line = '';
>open( INFILE, "< $infile" ) or die "Can't open $infile : $!";
>open( OUTFILE, "> $outfile" ) or die "Can't open $outfile : $!";
>while ($line = <INFILE>){
>  if ($line =~ /BAS-/)
>    {
>      my $IP = '';
>      my $Address = '';
>      my $Name = '';
>      my $mac = '';
>      my $type = '';
>      my $J6 = '';
>      my $J7 = '';
>      my $J8 = '';
>      my $J9 = '';
>      my $J10 = '';
>      my $J11 = '';
>      my $J12 = '';
>      my $J13 = '';
>      my $J14 = '';
>      my $J15 = '';
>      my $J16 = '';

This whole marathon above is unnecessary. 
- for the declaration just add a "my" in front of the opening
paranthesis in the next line and Perl will declare all those variables
in the list in one single go.
- and the initializations are pointless, too, because you overwrite
those empty strings in the next statement without ever using them.

>      ($IP,$Address,$Name,$mac,$type,$J6,$J7,$J8,$J9,$J10,$J11,$J12,$J13,
>$J14,$J15,$J16) = split '\t',$line;

You are never using any of those values except for $IP. Why are you
extracting and even naming them? There are several ways to get exactly
the one value you are interested in:
- split() returns a list, therefore you can index exactly the one value
you are looking for:
	my $IP = (split/\t/)[0];

- as you happen do be interested in the first element only you can
simply just catch that one and let all the others fall into the bit
bucket:
	my ($IP) = split/\t/;

- if you really need a dummy to catch some unwanted values you should
use 'undef' instead of a named variable:
	($IP, undef, undef, $otherValue) = split ....
will make it obvious that you are interested in the first and fourth
value anly and are deliberately ignoring the second and third.
Ov course this could also be written as
	my ($IP, $otherValue) = (split/\t/)[0,3];

- sometimes it is nice to use the split limit:
	($IP, undef) = split /\t/, $_, 2;
makes it very obvious that I am interested in the first element only and
are deliberately ignoring whatever else is following in that line.
  
jue


------------------------------

Date: Thu, 04 Mar 2010 11:16:28 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <slrnhovqg8.jh3.tadmc@tadbox.sbcglobal.net>

Don Pich <dpich@polartel.com> wrote:

> open( INFILE, "< $infile" ) or die "Can't open $infile : $!";


Using the 3-arg form of open() is much safer.

Using lexical filehandles is also a good idea.

Putting some delimiters around the filename in the die message
will be helpful if whitespace should ever sneak into the filename.

So that should be:

    open( my $INFILE, '<',  $infile ) or die "Can't open '$infile' $!";


> while ($line = <INFILE>){


    while ($line = <$INFILE>) {


>       my $IP = '';
>       my $Address = '';
>       my $Name = '';
>       my $mac = '';
>       my $type = '';
>       my $J6 = '';
>       my $J7 = '';
>       my $J8 = '';
>       my $J9 = '';
>       my $J10 = '';
>       my $J11 = '';
>       my $J12 = '';
>       my $J13 = '';
>       my $J14 = '';
>       my $J15 = '';
>       my $J16 = '';
>       ($IP,$Address,$Name,$mac,$type,$J6,$J7,$J8,$J9,$J10,$J11,$J12,$J13,
> $J14,$J15,$J16) = split '\t',$line;


Replace those 17 lines of code with 1 line of code:

    my($IP) = split /\t/, $line;

since you never make use of the other 15 variables.


> Here is the input file 'Pannaway.txt':


You should use the __DATA__ filehandle when posting file
contents to newsgroups.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


------------------------------

Date: Thu, 04 Mar 2010 11:41:35 -0600
From: Don Pich <dpich@polartel.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <So2dnSJLTeBSbRLWnZ2dnUVZ_hadnZ2d@polarcomm.com>

Alright,

Here is my shortened script:

#!/usr/bin/perl
use strict;
use warnings;
my $infile = 'Pannaway.txt';
my $outfile = 'BASIP.list';
my $line = '';
open( my $INFILE, '<',  $infile ) or die "Can't open '$infile' $!";
open( OUTFILE, "> $outfile" ) or die "Can't open $outfile : $!\n";
while ($line = <$INFILE>) {
      my($IP) = split /\t/, $line;
      foreach my $val (@value) {
	$val = sprintf("%02x", $val);
	printf OUTFILE $val;
      }
}
close (INFILE); close (OUTFILE);

But it doesn't run because of:

Global symbol "@value" requires explicit package name at ./convertX.pl 
line 12.
Execution of ./convertX.pl aborted due to compilation errors.


------------------------------

Date: Thu, 04 Mar 2010 11:53:51 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <874okwvuq8.fsf@castleamber.com>

Tad McClellan <tadmc@seesig.invalid> writes:

> Don Pich <dpich@polartel.com> wrote:
>
>> open( INFILE, "< $infile" ) or die "Can't open $infile : $!";
>
>
> Using the 3-arg form of open() is much safer.
>
> Using lexical filehandles is also a good idea.
>
> Putting some delimiters around the filename in the die message
> will be helpful if whitespace should ever sneak into the filename.

I also prefer to add what I tried to do (for reading):

   .... or die "Can't open '$infile' for reading: $!";

-- 
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development


------------------------------

Date: Thu, 04 Mar 2010 11:58:57 -0600
From: Don Pich <dpich@polartel.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <So2dnV1LTeB8aRLWnZ2dnUVZ_hYAAAAA@polarcomm.com>

Tad,

Current Script:

#!/usr/bin/perl
use strict;
use warnings;
my $infile = 'Pannaway.txt';
my $outfile = 'BASIP.list';
my $line = '';
open( my $INFILE, '<',  $infile ) or die "Can't open '$infile' $!";
open( OUTFILE, "> $outfile" ) or die "Can't open $outfile : $!\n";
while ($line = <$INFILE>) {
      my(@IP) = split /\t/, $line;
      foreach my $val (@IP) {
	$val = sprintf("%02x", $val);
	printf OUTFILE $val;
      }
}
close (INFILE); close (OUTFILE);

Source File (snippet):
10.20.150.252	STTM.12.01	BAS-ADSL16R	00:0A:9F:40:71:D3	
3.2.1.28							
10.20.150.250	STTM.13.01	BAS-ADSL16R	00:0A:9F:40:71:DF	
3.2.1.28							
10.20.151.252	STTM.14.01	BAS-ADSL16R	00:0A:9F:40:71:CD	
3.2.1.28							
10.20.151.250	STTM.15.01	BAS-ADSL48R	00:0A:9F:40:67:05	
3.2.1.28							
10.20.151.248	STTM.16.01	BAS-ADSL48R	00:0A:9F:40:6D:19	
3.2.1.28							
10.20.145.252	STTM.41.01 CO	BAS-ADSL48R	00:0A:9F:40:67:15	
3.2.1.28							
10.20.145.250	STTM.41.02 CO	BAS-ADSL48R	00:0A:9F:00:E5:72	
3.2.1.28							
10.20.128.29	STTM.BAR.01	BAR-GE12	00:0A:9F:50:0D:38	
02.02.00.22							
10.20.128.22	STTM.BAR.02	BAR-GE12	00:0A:9F:50:5E:E9	
02.02.00.22							:

Desired output:

convert the IP into a quad hex form.

I ran the application by inputing "perl file.pl"


------------------------------

Date: Thu, 04 Mar 2010 10:14:46 -0800
From: sln@netherlands.com
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <astvo5dmjesggsbu8jg7g5bsk72atdfspv@4ax.com>

On Thu, 04 Mar 2010 10:17:04 -0600, Don Pich <dpich@polartel.com> wrote:

>Here is my script:
>
>**  Notice the extra line after 0a1491fa
>
>I realize that after the field "IP" is converted to hex, it gets out of 
>the "foreach" loop and then prints the carriage return to start the next 
>line in the file.  Can I redo this script to not have it create that last 
>carriage return?  Can I place it in another spot so that it doesn't 
>create the extra blank line?

What makes you think there is an extra line after 0a1491fa ??
Newline is the same character appended to each ip string, its part of
the IP string isin't it?

Without it, you don't know where a valid end of the IP string is.

-sln


------------------------------

Date: Thu, 04 Mar 2010 10:18:02 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <29uvo5lklcd8gcncolt17q6vufk4q3avl2@4ax.com>

Don Pich <dpich@polartel.com> wrote:
[...]>while ($line = <$INFILE>) {
>      my($IP) = split /\t/, $line;
>      foreach my $val (@value) {
[...]
>But it doesn't run because of:
>
>Global symbol "@value" requires explicit package name at ./convertX.pl 
>line 12.

Well, yeah, you never declared nor defined @value. What is that array
supposed to be?

jue


------------------------------

Date: Thu, 04 Mar 2010 10:23:10 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <ccuvo5lt7r58shb2599840r3pem2n8i750@4ax.com>

Don Pich <dpich@polartel.com> wrote:
>while ($line = <$INFILE>) {
>      my(@IP) = split /\t/, $line;

????
You are confusing me. According to your sample data each line contains
IP, address, name, MAC and a whole jumbalaja of other stuff. Why are you
putting all of that into an array names @IP?

>      foreach my $val (@IP) {
>	$val = sprintf("%02x", $val);

And why are you trying to format the address, name, MAC, ... as hex
values?

>Source File (snippet):
>10.20.150.252	STTM.12.01	BAS-ADSL16R	00:0A:9F:40:71:D3	
>3.2.1.28							
>10.20.150.250	STTM.13.01	BAS-ADSL16R	00:0A:9F:40:71:DF	
>3.2.1.28							

jue


------------------------------

Date: Thu, 4 Mar 2010 10:33:38 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <eb9fb6e4-181a-495f-aed0-4137da81ad0c@q23g2000yqd.googlegroups.com>

On Mar 4, 1:14=A0pm, s...@netherlands.com wrote:
> Without it, you don't know where a valid end of the IP string is.

What about EOF? Surely a line not terminated by a newline but followed
by an EOF has a valid ending.

CC


------------------------------

Date: Thu, 04 Mar 2010 10:43:38 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <2mvvo5pt2gucur71udgf0oofh1371amcmo@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:
>On Mar 4, 1:14 pm, s...@netherlands.com wrote:
>> Without it, you don't know where a valid end of the IP string is.
>
>What about EOF? Surely a line not terminated by a newline but followed
>by an EOF has a valid ending.

Actually there are many people who would argue that a 'line' without a
trailing newline (EOL) isn't a line at all but garbage at the end of the
file after the end of the preceeding line.

jue


------------------------------

Date: Thu, 04 Mar 2010 12:54:54 -0600
From: Don Pich <dpich@polartel.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <So2dnVxLTeBjnA3WnZ2dnUVZ_hadnZ2d@polarcomm.com>

You know what?  My completely ugly script actually did what it was 
supposed to.  This was a one time shot and probably will not have to run 
this again in the near future.  So I think I will call off the hunt on 
getting this going.

I'm VERY new at trying to use Perl.  I've used other programing languages 
before.  But I'm definitely in my infancy at trying to understand.  

If I post to this forum again, I will try to be more specific as not to 
cause confusion.

Thanks to all who helped me as far as they did!!!


------------------------------

Date: Thu, 04 Mar 2010 12:58:20 -0600
From: Don Pich <dpich@polartel.com>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <So2dnV5LTeBRnw3WnZ2dnUVZ_hadnZ2d@polarcomm.com>

Hey Jue,

The reason I have to do that is I have a dslam that is sending option82 
data as a complete hex string, part of which is the ip address in a hex 
form.  In order for me to authenticate against the circuit-id, I need to 
add an ldap entry for each dslam using the hex value of the ip address.


------------------------------

Date: Thu, 4 Mar 2010 19:00:14 +0000 (UTC)
From: Willem <willem@snail.stack.nl>
Subject: Re: Prevent script from adding an extra line at the end of file
Message-Id: <slrnhp00pu.up4.willem@snail.stack.nl>

Don Pich wrote:
) Here is my script:
)
)  <snip>
)
) Here is the output file BASIP.list:
) 0a1497fa
) 0a1497f8
) 0a1491fc
) 0a1491fa
)
) **  Notice the extra line after 0a1491fa

What extra line ?  Is there \n\n at the end of the file ?
As far as I can tell, your script prints one newline after each line.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


------------------------------

Date: Thu, 4 Mar 2010 10:22:05 -0000
From: "John" <john1949@yahoo.com>
Subject: regex match on nothing
Message-Id: <hmo1kd$glb$1@news.albasani.net>

Hi

my $x='Fred&#39;s book';
or $x="Fred's book";
or $x-'Freds book';

if ($x =~ s /Fred(&#39;|\')s book/xxxxx/);

How can I modify this when there is nothing between Fred and s?

Regards
John







------------------------------

Date: Thu, 04 Mar 2010 12:52:49 +0100
From: Josef Moellers <josef.moellers@ts.fujitsu.com>
Subject: Re: regex match on nothing
Message-Id: <hmo6uh$dmt$1@nntp.fujitsu-siemens.com>

John wrote:
> Hi
> 
> my $x='Fred&#39;s book';
> or $x="Fred's book";
> or $x-'Freds book';
> 
> if ($x =~ s /Fred(&#39;|\')s book/xxxxx/);
> 
> How can I modify this when there is nothing between Fred and s?

Just put a question mark after the closing parenthesis:

    if ($x =~ s/Fred(&#39;|\')?s book/xxxxx/) {


-- 
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html


------------------------------

Date: Thu, 04 Mar 2010 07:33:58 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: regex match on nothing
Message-Id: <HpQjn.4185$3D3.2235@newsfe19.iad>

John wrote:
> Hi
> 
> my $x='Fred&#39;s book';
> or $x="Fred's book";
> or $x-'Freds book';
> 
> if ($x =~ s /Fred(&#39;|\')s book/xxxxx/);
> 
> How can I modify this when there is nothing between Fred and s?

Add a third alternative, zero-width match:

if ( $x =~ s/Fred(?:&#39;|'|)s book/xxxxx/ )



John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway


------------------------------

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 2850
***************************************


home help back first fref pref prev next nref lref last post