[31027] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2272 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 13 03:09:41 2009

Date: Fri, 13 Mar 2009 00:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 13 Mar 2009     Volume: 11 Number: 2272

Today's topics:
    Re: Autovivification by foreach <devnull4711@web.de>
        capturing multiple statement, which starts with "msg_pr <guru.naveen@gmail.com>
    Re: capturing multiple statement, which starts with "ms <someone@example.com>
    Re: Interactive App over Telnet <maustin@firstdbasource.com>
        new CPAN modules on Fri Mar 13 2009 (Randal Schwartz)
        Perl Pipes alertjean@rediffmail.com
    Re: Perl Pipes <jurgenex@hotmail.com>
    Re: Perl Pipes <tadmc@seesig.invalid>
        Why does this not print the filenames ? <elhmbre@ozemail.com.au>
    Re: Why does this not print the filenames ? <jurgenex@hotmail.com>
    Re: Why does this not print the filenames ? <jurgenex@hotmail.com>
    Re: Why does this not print the filenames ? <noreply@gunnar.cc>
    Re: Why does this not print the filenames ? <elhmbre@ozemail.com.au>
    Re: Why does this not print the filenames ? <maustin@firstdbasource.com>
    Re: Why does this not print the filenames ? <noreply@gunnar.cc>
    Re: Why does this not print the filenames ? <elhmbre@ozemail.com.au>
    Re: Why does this not print the filenames ? <noreply@gunnar.cc>
    Re: Why does this not print the filenames ? <tadmc@seesig.invalid>
    Re: Why does this not print the filenames ? <someone@example.com>
    Re: Why does this not print the filenames ? <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 13 Mar 2009 07:38:02 +0100
From: Frank Seitz <devnull4711@web.de>
Subject: Re: Autovivification by foreach
Message-Id: <71udaaFn4bctU1@mid.individual.net>

Uri Guttman wrote:
>>>>>> "FS" == Frank Seitz <devnull4711@web.de> writes:
> 
>   FS> my (@a,$ref);
>   FS> push @a,@$ref;
> 
>   FS> Perl throws an exception:
>   FS> Can't use an undefined value as an ARRAY reference
>   FS> Seems plausible.
> 
>   FS> We are modifying the code:
> 
>   FS> my (@a,$ref);
>   FS> push @a,$_ for @$ref;
> 
>   FS> Now, there is no exception anymore. Perl creates the array on the fly.
>   FS> Why is it so? What is the difference?
> 
> in the first case @$ref is readonly as it is input to push and can't be
> modified. autovivification only happens on lvalues when they are undef
> and used where a ref should be.

Thanks, the readonly/lvalue distinction in this context was not clear to me.

> on the other hand for (modifier or main loop style) makes $_ an alias
> into each element of its list. and $_ is read/write so the elements
> should be lvalues. so perl will autovivify the array ref since you might
> be modifying elements. it can't tell (the executed code could be a sub
> call, etc.) so it has to do this before the loop starts.

This explanation is not plausible to me. Perl autovivifies before
loop start, because the loop might be modifying array elements?
How is this possible? The autovivificated array is empty.
It is clear that the body of the loop is never executed.

In my opinion it would make more sense when Perl would
throw an exception in this case too.

Frank
-- 
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel


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

Date: Thu, 12 Mar 2009 21:07:09 -0700 (PDT)
From: guru <guru.naveen@gmail.com>
Subject: capturing multiple statement, which starts with "msg_pri"
Message-Id: <3b8a39e9-f269-4fc4-971c-fe5f4aad9a26@r15g2000prh.googlegroups.com>


Hi All,

I have one requirement where I need to capture statement beginning
with "msg_pri". This may span multiple lines.

Cases
1. msg_pri (........................);
2. msg_pri (........................)
3. msg_pri (........................) \

other cases
same as above , but they may span multiple lines.

4. msg_pri (........................,
                       ..............);

Below perl script convert multiple line msg_pri in to single line and
handling case 1 and case 2, 4.

It is not handling the case 3. It will hang when it encounters case 3.

system("perl -i -ne  \' unless(<EOF>){ if(/msg_pri.?\\(/i) { until(/[\;
\)]\$/) { chomp; s/\\s+/ /; print; \$_ = <>;} s/\\s+/ /;} print }\'
$process_file");

I modified "until(/[\;\)]\$/" to "until(/[\\\;\)]\$" in the above
statement then also it is not handling.

How to modify this to handle all the above cases or any other way to
do this in perl.

Please let me know if you need any other info.

Thanks & Regards
Gururaja


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

Date: Thu, 12 Mar 2009 22:17:40 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: capturing multiple statement, which starts with "msg_pri"
Message-Id: <V%lul.117549$xK6.5842@newsfe12.iad>

guru wrote:
> 
> I have one requirement where I need to capture statement beginning
> with "msg_pri". This may span multiple lines.
> 
> Cases
> 1. msg_pri (........................);
> 2. msg_pri (........................)
> 3. msg_pri (........................) \
> 
> other cases
> same as above , but they may span multiple lines.
> 
> 4. msg_pri (........................,
>                        ..............);
> 
> Below perl script convert multiple line msg_pri in to single line and
> handling case 1 and case 2, 4.
> 
> It is not handling the case 3. It will hang when it encounters case 3.
> 
> system("perl -i -ne  \' unless(<EOF>){ if(/msg_pri.?\\(/i) { until(/[\;

Why system?  Are you running this in perl as a separate process, and if 
so why?  Where did the EOF filehandle come from?  It is not doing 
anything in your example.


> \)]\$/) { chomp; s/\\s+/ /; print; \$_ = <>;} s/\\s+/ /;} print }\'
> $process_file");

Converting that to usable Perl code:

local ( $^I, @ARGV ) = ( '', $process_file );
while ( <> ) {
     unless ( <EOF> ) {
         if ( /msg_pri.?\(/i ) {
             until ( /[;)]$/ ) {
                 chomp;
                 s/\s+/ /;
                 print;
                 $_ = <>;
                 }
             s/\s+/ /;
             }
         print
         }
     }


> I modified "until(/[\;\)]\$/" to "until(/[\\\;\)]\$" in the above
> statement then also it is not handling.
> 
> How to modify this to handle all the above cases or any other way to
> do this in perl.
> 
> Please let me know if you need any other info.

What determines "a single line"?  What do you want the output to look like?



John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


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

Date: Thu, 12 Mar 2009 22:49:43 -0500
From: Michael Austin <maustin@firstdbasource.com>
Subject: Re: Interactive App over Telnet
Message-Id: <IHkul.26077$ZP4.24146@nlpi067.nbdc.sbc.com>

Shawn N Blank wrote:
> On Thu, 12 Mar 2009 15:51:38 GMT, sln@netherlands.com wrote:
> 
>> I would use Tera Term. 
> 
> I'll take a look and see what it can do.
> 
> --
> Shawn


you might also investigate using ssh vs. telnet - unless you don't care 
if your pwd is passed in clear text.




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

Date: Fri, 13 Mar 2009 04:42:25 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Fri Mar 13 2009
Message-Id: <KGFH2p.17Kz@zorch.sf-bay.org>

The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN).  You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.

App-Toodledo-0.02
http://search.cpan.org/~pjs/App-Toodledo-0.02/
Interacting with the Toodledo task management service. 
----
Bundle-Simo-0.0101
http://search.cpan.org/~kimoto/Bundle-Simo-0.0101/
A bundle to install Simo related modules 
----
Business-KontoCheck-2.97
http://search.cpan.org/~michel/Business-KontoCheck-2.97/
Perl extension for checking German and Austrian Bank Account Numbers 
----
CPAN-PackageDetails-0.18
http://search.cpan.org/~bdfoy/CPAN-PackageDetails-0.18/
Create or read 02packages.details.txt.gz 
----
Catalyst-Manual-5.7020
http://search.cpan.org/~hkclark/Catalyst-Manual-5.7020/
The Catalyst developer's manual 
----
Chart-Gnuplot-0.08
http://search.cpan.org/~kwmak/Chart-Gnuplot-0.08/
Plot graph using Gnuplot on the fly 
----
Chart-OFC2-0.03_03
http://search.cpan.org/~jkutej/Chart-OFC2-0.03_03/
Generate html and data files for use with Open Flash Chart version 2 
----
Config-Auto-0.29_02
http://search.cpan.org/~kane/Config-Auto-0.29_02/
Magical config file parser 
----
Config-Model-TkUI-1.207
http://search.cpan.org/~ddumont/Config-Model-TkUI-1.207/
Tk GUI to edit config data through Config::Model 
----
Email-MIME-Kit-2.003
http://search.cpan.org/~rjbs/Email-MIME-Kit-2.003/
build messages from templates 
----
Email-MIME-Kit-Assembler-TextifyHTML-1.000
http://search.cpan.org/~rjbs/Email-MIME-Kit-Assembler-TextifyHTML-1.000/
textify some HTML arguments to assembly 
----
Email-MIME-Kit-Renderer-Text-Template-1.000
http://search.cpan.org/~rjbs/Email-MIME-Kit-Renderer-Text-Template-1.000/
render parts of your mail with Text::Template 
----
File-Lock-Multi-0.02
http://search.cpan.org/~crakrjack/File-Lock-Multi-0.02/
Lock files more than once 
----
Getopt-Lucid-0.18
http://search.cpan.org/~dagolden/Getopt-Lucid-0.18/
Clear, readable syntax for command line processing 
----
GitStore-0.01_01
http://search.cpan.org/~fayland/GitStore-0.01_01/
Git as versioned data store in Perl 
----
Guardian-OpenPlatform-API-0.02
http://search.cpan.org/~davecross/Guardian-OpenPlatform-API-0.02/
Access the Guardian OpenPlatform API 
----
Guardian-OpenPlatform-API-0.03
http://search.cpan.org/~davecross/Guardian-OpenPlatform-API-0.03/
Access the Guardian OpenPlatform API 
----
Guardian-OpenPlatform-API-0.04
http://search.cpan.org/~davecross/Guardian-OpenPlatform-API-0.04/
Access the Guardian OpenPlatform API 
----
HTML-Shakan-0.01_04
http://search.cpan.org/~tokuhirom/HTML-Shakan-0.01_04/
form html generator/validator 
----
Hessian-Client-0.1.5
http://search.cpan.org/~heytrav/Hessian-Client-0.1.5/
RPC via Hessian with a remote server. 
----
Module-Manifest-0.06
http://search.cpan.org/~adamk/Module-Manifest-0.06/
Parse and examine a Perl distribution MANIFEST file 
----
MouseX-AttributeHelpers-0.02
http://search.cpan.org/~masaki/MouseX-AttributeHelpers-0.02/
Extend your attribute interfaces 
----
MyCPAN-Indexer-1.18
http://search.cpan.org/~bdfoy/MyCPAN-Indexer-1.18/
Index a Perl distribution 
----
MyCPAN-Indexer-1.19
http://search.cpan.org/~bdfoy/MyCPAN-Indexer-1.19/
Index a Perl distribution 
----
Net-Async-FTP-0.02
http://search.cpan.org/~pevans/Net-Async-FTP-0.02/
Asynchronous FTP client 
----
Net-TacacsPlus-1.08
http://search.cpan.org/~jkutej/Net-TacacsPlus-1.08/
Tacacs+ library 
----
News-Pictures-0.02
http://search.cpan.org/~cguine/News-Pictures-0.02/
The great new News::Pictures! 
----
Object-LocalVars-0.17
http://search.cpan.org/~dagolden/Object-LocalVars-0.17/
Outside-in objects with local aliasing of $self and object variables 
----
POE-Component-PubSub-0.03
http://search.cpan.org/~nperez/POE-Component-PubSub-0.03/
A generic publish/subscribe POE::Component that enables POE::Sessions to publish events to which other POE::Sessions may subscribe. 
----
POE-Component-SmokeBox-0.18
http://search.cpan.org/~bingos/POE-Component-SmokeBox-0.18/
POE enabled CPAN smoke testing with added value. 
----
POE-Declare-0.14
http://search.cpan.org/~adamk/POE-Declare-0.14/
A POE abstraction layer for conciseness and simplicity 
----
POE-Declare-0.15
http://search.cpan.org/~adamk/POE-Declare-0.15/
A POE abstraction layer for conciseness and simplicity 
----
POE-Declare-0.16
http://search.cpan.org/~adamk/POE-Declare-0.16/
A POE abstraction layer for conciseness and simplicity 
----
POE-Declare-0.18
http://search.cpan.org/~adamk/POE-Declare-0.18/
A POE abstraction layer for conciseness and simplicity 
----
RDF-Query-2.003_02
http://search.cpan.org/~gwilliams/RDF-Query-2.003_02/
An RDF query implementation of SPARQL/RDQL in Perl for use with RDF::Redland and RDF::Core. 
----
RT-Extension-ExtractCustomFieldValues-2.05
http://search.cpan.org/~alexmv/RT-Extension-ExtractCustomFieldValues-2.05/
----
RT-Extension-ExtractCustomFieldValues-2.99_01
http://search.cpan.org/~alexmv/RT-Extension-ExtractCustomFieldValues-2.99_01/
----
SNA-Network-0.02
http://search.cpan.org/~obradovic/SNA-Network-0.02/
A toolkit for Social Network Analysis 
----
Simo-0.1003
http://search.cpan.org/~kimoto/Simo-0.1003/
Very simple framework for Object Oriented Perl. 
----
Simo-0.1004
http://search.cpan.org/~kimoto/Simo-0.1004/
Very simple framework for Object Oriented Perl. 
----
Statistics-Zed-0.02
http://search.cpan.org/~rgarton/Statistics-Zed-0.02/
Basic ztest/zscore, with optional continuity correction, Fisher's r-to-z, z-to-r, et al. 
----
TaskForest-1.21
http://search.cpan.org/~enoor/TaskForest-1.21/
A simple but expressive job scheduler that allows you to chain jobs/tasks and create time dependencies. Uses text config files to specify task dependencies. 
----
Template-Plugin-Latex-3.02
http://search.cpan.org/~andrewf/Template-Plugin-Latex-3.02/
Template Toolkit plugin for Latex 
----
Test-DistManifest-1.1.0
http://search.cpan.org/~frequency/Test-DistManifest-1.1.0/
Tests that your MANIFEST matches the distribution as it exists, excluding those in your MANIFEST.SKIP 
----
Test-Fixme-0.04
http://search.cpan.org/~evdb/Test-Fixme-0.04/
check code for FIXMEs. 
----
Test-Server-0.05
http://search.cpan.org/~jkutej/Test-Server-0.05/
what about test driven administration? 
----
Test-Sys-Info-0.10
http://search.cpan.org/~burak/Test-Sys-Info-0.10/
Centralized test suite for Sys::Info. 
----
Test-TT-0.01
http://search.cpan.org/~bolav/Test-TT-0.01/
Test::More-style wrapper around Template 
----
Text-Capitalize-0.6
http://search.cpan.org/~doom/Text-Capitalize-0.6/
capitalize strings ("to WORK AS titles" becomes "To Work as Titles") 
----
WWW-TV-0.14
http://search.cpan.org/~tigris/WWW-TV-0.14/
Parse TV.com for information about TV shows. 
----
WebService-Careerjet-0.08
http://search.cpan.org/~jeteve/WebService-Careerjet-0.08/
Perl interface to Careerjet's public search API 
----
WebService-Nestoria-Search-1.14.6
http://search.cpan.org/~kaoru/WebService-Nestoria-Search-1.14.6/
Perl interface to the Nestoria Search public API. 
----
XML-RSS-FromHTML-0.03
http://search.cpan.org/~bashi/XML-RSS-FromHTML-0.03/
simple framework for making RSS out of HTML 
----
XML-RSS-FromHTML-0.031
http://search.cpan.org/~bashi/XML-RSS-FromHTML-0.031/
simple framework for making RSS out of HTML 
----
ex-lib-0.05
http://search.cpan.org/~mons/ex-lib-0.05/
The same as lib, but makes relative path absolute. 


If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.

This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
  http://www.stonehenge.com/merlyn/LinuxMag/col82.html

print "Just another Perl hacker," # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

Date: Thu, 12 Mar 2009 16:12:37 -0700 (PDT)
From: alertjean@rediffmail.com
Subject: Perl Pipes
Message-Id: <9d128d2c-df7a-4aca-9207-6c897781e1c1@40g2000yqe.googlegroups.com>

Fellow script writers,
I have two linux executables. I want to give the stand ouput  of
executable1 to executable 2.
How can I do it with perl without creating intermediate files ?

something which can be achieved through command line.

$ executable1 > output_file1.txt
$  executable2  output_file1.txt  > output_file2.txt


regards
Jean




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

Date: Thu, 12 Mar 2009 17:03:19 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Perl Pipes
Message-Id: <0h8jr4puqkcvc93tvssg6sq5nuh639idjn@4ax.com>

alertjean@rediffmail.com wrote:
>I have two linux executables. I want to give the stand ouput  of
>executable1 to executable 2.
>How can I do it with perl without creating intermediate files ?
>
>something which can be achieved through command line.
>
>$ executable1 > output_file1.txt
>$  executable2  output_file1.txt  > output_file2.txt

Are you looking for something different then a trivial

	system ('executable1 | executable2  > output_file2.txt');

jue


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

Date: Thu, 12 Mar 2009 19:01:53 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Perl Pipes
Message-Id: <slrngrj8jh.rfb.tadmc@tadmc30.sbcglobal.net>

alertjean@rediffmail.com <alertjean@rediffmail.com> wrote:
> Fellow script writers,
> I have two linux executables. I want to give the stand ouput  of
> executable1 to executable 2.
> How can I do it with perl without creating intermediate files ?
>
> something which can be achieved through command line.
>
> $ executable1 > output_file1.txt
> $  executable2  output_file1.txt  > output_file2.txt


The simplest way is to let the shell do it for you:

    system 'executable1 | executable2 >output_file2.txt';


Or, if you want to capture the end of the pipeline into your Perl
program rather than into a file:

    open my $pipeline, 'executable1 | executable2 |' or die...

Then you can read the data with:

    while ( <$pipeline> ) {
        ...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Fri, 13 Mar 2009 11:19:21 +1000
From: "elhombre" <elhmbre@ozemail.com.au>
Subject: Why does this not print the filenames ?
Message-Id: <49b9b4a3$0$5641$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

I'm sure this is a typical newbie error but I cannot see why this prints

FILE wordcount is 17  when it should print c:\original.txt word count is 17.

If I try

print $_ . " word count is " . $wordCount . "\n";

it complains that $_ is unitialised and I cannot see why.

Any ideas ?


foreach (@ARGV) {
    open(FILE, $_) or die "File $_ does not exist";
    while (<FILE>) {
        my @words = split;
        foreach my $word (@words) {
            $wordCount++;
        }
    }
    print FILE  . " word count is " . $wordCount . "\n";
    close FILE;
    $wordCount = 0;
} 



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

Date: Thu, 12 Mar 2009 18:33:36 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Why does this not print the filenames ?
Message-Id: <0ndjr4pil6aldf4o62hp10sheo6bpjccef@4ax.com>

"elhombre" <elhmbre@ozemail.com.au> wrote:
>I'm sure this is a typical newbie error but I cannot see why this prints
>
>FILE wordcount is 17  when it should print c:\original.txt word count is 17.
>
>If I try
>
>print $_ . " word count is " . $wordCount . "\n";
>
>it complains that $_ is unitialised and I cannot see why.
>
>Any ideas ?
>
>
>foreach (@ARGV) {
>    open(FILE, $_) or die "File $_ does not exist";
>    while (<FILE>) {
>        my @words = split;
>        foreach my $word (@words) {
>            $wordCount++;

The whole while() loop can better be written as 
	$wordcount = @words;
because an array used in scalar context returns the number of its
elements.No need to manually count them.

And that will also solve the warning message, because you are using $_
for two conflicting purposes: to iterate over @ARGV and to iterate over
@words.

>        }
>    }
>    print FILE  . " word count is " . $wordCount . "\n";

You aren't printing $_, you are printing FILE.

>    close FILE;
>    $wordCount = 0;
>} 

jue


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

Date: Thu, 12 Mar 2009 18:41:47 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Why does this not print the filenames ?
Message-Id: <s9ejr4tofeg5nj8vfe20ps1vud2bqu4gc0@4ax.com>

Jürgen Exner <jurgenex@hotmail.com> wrote:
>"elhombre" <elhmbre@ozemail.com.au> wrote:

>>foreach (@ARGV) {
>>    open(FILE, $_) or die "File $_ does not exist";
>>    while (<FILE>) {
>>        my @words = split;
>>        foreach my $word (@words) {
>>            $wordCount++;
>
>The whole while() loop can better be written as 

Daaah! 
Note to self: Before posting turn on brain.

Make that the whole foreach() loop could be written as 
	$wordCount +=  @words;

>because an array used in scalar context returns the number of its
>elements.No need to manually count them.

jue


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

Date: Fri, 13 Mar 2009 02:49:48 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Why does this not print the filenames ?
Message-Id: <71tse2Fmev9fU1@mid.individual.net>

elhombre wrote:
> I'm sure this is a typical newbie error but I cannot see why this prints
> 
> FILE wordcount is 17  when it should print c:\original.txt word count is 
> 17.

Well, I don't understand how you got that far with the code below. When 
I tried it, Perl complained with the error message "Modification of a 
read-only value attempted".

Only after having changed

>    while (<FILE>) {
>        my @words = split;

to

    while ( my $line = <FILE> ) {
        my @words = split ' ', $line;

the script produces the output you say.

> If I try
> 
> print $_ . " word count is " . $wordCount . "\n";
> 
> it complains that $_ is unitialised and I cannot see why.

With the above change, it does not complain.

> Any ideas ?

You are trying to alias $_ to both a file name and a line. In cases when 
you have an outer and an inner loop, you need to be specific in at least 
one of them.

> foreach (@ARGV) {
>    open(FILE, $_) or die "File $_ does not exist";
>    while (<FILE>) {
>        my @words = split;
>        foreach my $word (@words) {
>            $wordCount++;
>        }
>    }
>    print FILE  . " word count is " . $wordCount . "\n";
>    close FILE;
>    $wordCount = 0;
> }

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Fri, 13 Mar 2009 11:53:57 +1000
From: "elhombre" <elhmbre@ozemail.com.au>
Subject: Re: Why does this not print the filenames ?
Message-Id: <49b9bcbf$0$5582$5a62ac22@per-qv1-newsreader-01.iinet.net.au>


"Jürgen Exner" <jurgenex@hotmail.com> wrote in message 
news:0ndjr4pil6aldf4o62hp10sheo6bpjccef@4ax.com...
> "elhombre" <elhmbre@ozemail.com.au> wrote:
> The whole while() loop can better be written as
> $wordcount = @words;
> because an array used in scalar context returns the number of its
> elements.No need to manually count them.
>
> And that will also solve the warning message, because you are using $_
> for two conflicting purposes: to iterate over @ARGV and to iterate over
> @words.
>
>>        }
>>    }
>>    print FILE  . " word count is " . $wordCount . "\n";
>
> You aren't printing $_, you are printing FILE.
>
>>    close FILE;
>>    $wordCount = 0;
>>}
>
> jue

Thanks Jue ! So simplifying gives me (I am going to rename FILE to FH so as 
to hopefully avoid confusion

foreach (@ARGV) {
    open(FH, $_) or die "File $_ does not exist";
    while (<FH>) {
        @words = split;
        $wordCount = @words;
    }
    print FH  . " word count is " . $wordCount . "\n";
    close FH;
}

Which gives the output

FH word count is 18

when what I am trying to get is

c:\original.txt word count is 18.

Which indicates of course that the filehandle FH is not the actual name of 
the file being opened ? How do I get it to print "c:\original.txt" ?

Thanks again. 



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

Date: Thu, 12 Mar 2009 21:03:43 -0500
From: Michael Austin <maustin@firstdbasource.com>
Subject: Re: Why does this not print the filenames ?
Message-Id: <k8jul.18590$YU2.9597@nlpi066.nbdc.sbc.com>

elhombre wrote:
> I'm sure this is a typical newbie error but I cannot see why this prints
> 
> FILE wordcount is 17  when it should print c:\original.txt word count is 
> 17.
> 
> If I try
> 
> print $_ . " word count is " . $wordCount . "\n";
> 
> it complains that $_ is unitialised and I cannot see why.
> 
> Any ideas ?
> 
> 
> foreach (@ARGV) {
>    open(FILE, $_) or die "File $_ does not exist";
>    while (<FILE>) {
>        my @words = split;
>        foreach my $word (@words) {
>            $wordCount++;
>        }
>    }
>    print FILE  . " word count is " . $wordCount . "\n";
>    close FILE;
>    $wordCount = 0;
> }


made a few mods - this works better (see comments below... :)

use strict;

foreach (@ARGV) {
    my $file = $_;
    my ($wordCount,$word);

    open(FILE, $file) or die "File $file does not exist";
    while (<FILE>) {
        my @words = split;
        foreach my $word (@words) {
            $wordCount++;
                                  }
                    }
    print $file  . " word count is " . $wordCount . "\n";
    close FILE;
                  }


Try uncommenting the $file def and replace $_ with $file and see if you 
get any different results.

  perl g.pl login.com login.com y.com version.com
  word count is 370
  word count is 370
  word count is 14
  word count is 63

VS.

  perl g.pl login.com login.com y.com version.com
login.com word count is 370
login.com word count is 370
y.com word count is 14
version.com word count is 63


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

Date: Fri, 13 Mar 2009 03:03:08 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Why does this not print the filenames ?
Message-Id: <71tt74Fn20a9U1@mid.individual.net>

Jürgen Exner wrote:
> "elhombre" <elhmbre@ozemail.com.au> wrote:
>>
>> foreach (@ARGV) {
>>    open(FILE, $_) or die "File $_ does not exist";
>>    while (<FILE>) {
>>        my @words = split;
>>        foreach my $word (@words) {
>>            $wordCount++;

<corrected> (in accordance with your next message)
> The whole foreach() loop could be written as 
> 	$wordCount +=  @words;
</corrected>

> because an array used in scalar context returns the number of its
> elements.No need to manually count them.
> 
> And that will also solve the warning message, because you are using $_
> for two conflicting purposes: to iterate over @ARGV and to iterate over
> @words.

No, he is using $_ to iterate over @ARGV and to iterate over <FILE>, so 
that conflict still remains. (See my post.)

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Fri, 13 Mar 2009 12:09:50 +1000
From: "elhombre" <elhmbre@ozemail.com.au>
Subject: Re: Why does this not print the filenames ?
Message-Id: <49b9c078$0$5593$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

"Michael Austin" <maustin@firstdbasource.com> wrote in message 
news:k8jul.18590$YU2.9597@nlpi066.nbdc.sbc.com...

> made a few mods - this works better (see comments below... :)
>
> use strict;
>
> foreach (@ARGV) {
>    my $file = $_;
>    my ($wordCount,$word);
>
>    open(FILE, $file) or die "File $file does not exist";
>    while (<FILE>) {
>        my @words = split;
>        foreach my $word (@words) {
>            $wordCount++;
>                                  }
>                    }
>    print $file  . " word count is " . $wordCount . "\n";
>    close FILE;
>                  }
>
>
Thank you Michael ! I've spent 3 hours at this and it was something so 
simple. Thanks as well to Jue and Gunter, your help is greatly appreciated 
and very valuable. 



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

Date: Fri, 13 Mar 2009 03:28:35 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Why does this not print the filenames ?
Message-Id: <71tumqFn1npaU1@mid.individual.net>

Gunnar Hjalmarsson wrote:
> elhombre wrote:
>> I'm sure this is a typical newbie error but I cannot see why this prints
>>
>> FILE wordcount is 17  when it should print c:\original.txt word count 
>> is 17.
> 
> Well, I don't understand how you got that far with the code below. When 
> I tried it, Perl complained with the error message "Modification of a 
> read-only value attempted".
> 
> Only after having changed
> 
>>    while (<FILE>) {
>>        my @words = split;
> 
> to
> 
>    while ( my $line = <FILE> ) {
>        my @words = split ' ', $line;
> 
> the script produces the output you say.

After having corrected Jürgen, I have to correct myself as well. The 
above change is advisable; not for the reason I mentioned above, but to 
prevent the warning message when $_ is printed after the while loop.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Thu, 12 Mar 2009 21:16:11 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Why does this not print the filenames ?
Message-Id: <slrngrjgfb.sh2.tadmc@tadmc30.sbcglobal.net>

elhombre <elhmbre@ozemail.com.au> wrote:

> I'm sure this is a typical newbie error but I cannot see why this prints
>
> FILE wordcount is 17  when it should print c:\original.txt word count is 17.

>     open(FILE, $_) or die "File $_ does not exist";

>     print FILE  . " word count is " . $wordCount . "\n";


There is no way to get a filename from a filehandle.

There may, in fact, be no filename associated with a filehandle:

    open FILE, 'date|' or die "problem running date program $!";

What "filename" should FILE have now?


If you want to use the filename later, save it into a variable.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Thu, 12 Mar 2009 20:49:13 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: Why does this not print the filenames ?
Message-Id: <ZIkul.116954$2h5.42421@newsfe11.iad>

Tad J McClellan wrote:
> elhombre <elhmbre@ozemail.com.au> wrote:
> 
>> I'm sure this is a typical newbie error but I cannot see why this prints
>>
>> FILE wordcount is 17  when it should print c:\original.txt word count is 17.
> 
>>     open(FILE, $_) or die "File $_ does not exist";
> 
>>     print FILE  . " word count is " . $wordCount . "\n";
> 
> 
> There is no way to get a filename from a filehandle.

Unless you put it there in the first place:

use vars '$FILE';

$FILE = 'filename';

open FILE or die "$FILE: $!";


> There may, in fact, be no filename associated with a filehandle:
> 
>     open FILE, 'date|' or die "problem running date program $!";
> 
> What "filename" should FILE have now?
> 
> 
> If you want to use the filename later, save it into a variable.


John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


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

Date: Thu, 12 Mar 2009 20:53:56 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: Why does this not print the filenames ?
Message-Id: <nNkul.116961$2h5.114866@newsfe11.iad>

elhombre wrote:
> I'm sure this is a typical newbie error but I cannot see why this prints
> 
> FILE wordcount is 17  when it should print c:\original.txt word count is 
> 17.
> 
> If I try
> 
> print $_ . " word count is " . $wordCount . "\n";
> 
> it complains that $_ is unitialised and I cannot see why.
> 
> Any ideas ?
> 
> 
> foreach (@ARGV) {
>    open(FILE, $_) or die "File $_ does not exist";
>    while (<FILE>) {
>        my @words = split;
>        foreach my $word (@words) {
>            $wordCount++;
>        }
>    }
>    print FILE  . " word count is " . $wordCount . "\n";
>    close FILE;
>    $wordCount = 0;
> }

Try it like this:

while ( <> ) {
     $wc += split;
     eof && print "$ARGV word count is $wc\n" and $wc = 0;
     }



John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V11 Issue 2272
***************************************


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