Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

perl-beginner · Perl Beginners Mailing List

The Yahoo! Groups Product Blog

Check it out!

Group Information

  • Members: 3765
  • Category: Perl
  • Founded: Aug 2, 1998
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Messages

Advanced
Messages Help
Messages 26909 - 26940 of 27459   Oldest  |  < Older  |  Newer >  |  Newest
Messages: Show Message Summaries Sort by Date ^  
#26909 From: gopi chand <gopichand84@...>
Date: Sun May 30, 2010 4:13 am
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
gopichand84
Send Email Send Email
 
Hi Kenneth,

Answered inline with [me]



--- On Sat, 5/29/10, Kenneth Wolcott <kennethwolcott@...> wrote:

From: Kenneth Wolcott <kennethwolcott@...>
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a 
given directory
To: perl-beginner@yahoogroups.com
Date: Saturday, May 29, 2010, 8:54 PM







 









       On Sat, May 29, 2010 at 20:25, gopi chand <gopichand84@...> wrote:



>

>

> I just got this over the net ,tried to run with  perl grep.pl (?i)sorting

> .zip but didn't work.Can you pls help?

>

> use strict;

> use warnings;

> use Cwd;

>

> use File::Find;

>

> my $search_pattern=$ARGV[0];

> my $file_pattern =$ARGV[1];

>

> find(\&d, cwd);

>

> sub d {

>

> my $file = $File::Find::name;

>

> $file =~ s,/,\\,g;

>

> return unless -f $file;

> return unless $file =~ /$file_pattern/;

>

> open F, $file or print "couldn't open $file\n" && return;

>

> while (<F>) {

> if (my ($found) = m/($search_pattern)/o) {

> print "found $found in $file\n";

> last;

> }

> }

>

> close F;

> }

>



Did you look at the code that you grabbed off the 'net and make any

conclusions about how it works?[me]yup.I understand how it works



Do you know how to use this code?[me]I don't

Where you grabbed this code from, was there any documentation on how to

use it?

[me]No.

It would help if the indentation was still in the code :-(



Let's make your problem easier so that you could solve a sub-problem, then

later use the knowledge gained to solve the larger problem.



How would you print all of the filenames with a .zip suffix that were in

the same directory using perl?[me]The below code prints all the files with a
.zip extension but it's not doing a recursive search into subdirectories
  #!/usr/bin/perl -w

  @files = <*.zip>;
  foreach $file (@files) {
    print $file . "\n";
  }


I don't want to solve this problem *for* you; I want to *help* you solve

it *yourself* :-)



Ken



[Non-text portions of this message have been removed]

























[Non-text portions of this message have been removed]

#26910 From: Kenneth Wolcott <kennethwolcott@...>
Date: Sun May 30, 2010 4:34 am
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
kwolcott
Send Email Send Email
 
On Sat, May 29, 2010 at 21:13, gopi chand <gopichand84@...> wrote:

>
>
>
> Hi Kenneth,
>
> Answered inline with [me]
>
>
> --- On Sat, 5/29/10, Kenneth Wolcott
<kennethwolcott@...<kennethwolcott%40gmail.com>>
> wrote:
>
> From: Kenneth Wolcott <kennethwolcott@...<kennethwolcott%40gmail.com>
> >
> Subject: Re: [PBML] Script to print all the files with .zip extentsion in a
> given directory
> To: perl-beginner@yahoogroups.com <perl-beginner%40yahoogroups.com>
> Date: Saturday, May 29, 2010, 8:54 PM
>
>
>
>
> On Sat, May 29, 2010 at 20:25, gopi chand
<gopichand84@...<gopichand84%40yahoo.com>>
> wrote:
>
> >
>
> >
>
> > I just got this over the net ,tried to run with perl grep.pl (?i)sorting
>
> > .zip but didn't work.Can you pls help?
>
> >
>
> > use strict;
>
> > use warnings;
>
> > use Cwd;
>
> >
>
> > use File::Find;
>
> >
>
> > my $search_pattern=$ARGV[0];
>
> > my $file_pattern =$ARGV[1];
>
> >
>
> > find(\&d, cwd);
>
> >
>
> > sub d {
>
> >
>
> > my $file = $File::Find::name;
>
> >
>
> > $file =~ s,/,\\,g;
>
> >
>
> > return unless -f $file;
>
> > return unless $file =~ /$file_pattern/;
>
> >
>
> > open F, $file or print "couldn't open $file\n" && return;
>
> >
>
> > while (<F>) {
>
> > if (my ($found) = m/($search_pattern)/o) {
>
> > print "found $found in $file\n";
>
> > last;
>
> > }
>
> > }
>
> >
>
> > close F;
>
> > }
>
> >
>
> Did you look at the code that you grabbed off the 'net and make any
>
> conclusions about how it works?[me]yup.I understand how it works
>
> Do you know how to use this code?[me]I don't
>
>
> Where you grabbed this code from, was there any documentation on how to
>
> use it?
>
> [me]No.
>
>
> It would help if the indentation was still in the code :-(
>
> Let's make your problem easier so that you could solve a sub-problem, then
>
> later use the knowledge gained to solve the larger problem.
>
> How would you print all of the filenames with a .zip suffix that were in
>
> the same directory using perl?[me]The below code prints all the files with
> a .zip extension but it's not doing a recursive search into subdirectories
> #!/usr/bin/perl -w
>
> @files = <*.zip>;
> foreach $file (@files) {
>   print $file . "\n";
>
> }
>
> I don't want to solve this problem *for* you; I want to *help* you solve
>
> it *yourself* :-)
>
> Ken
>

   Ok, I think you solved the easier sub-problem just fine.

   What is the output that you obtain when you run the script that you found
on the 'net?

   I don't precisely understand how you invoked the script, perhaps there
were some unicode characters that displayed incorrectly in my gmail screen
that confused me, so show me the output that you say "didn't work".

   Please be precise what you mean about "didn't work".

Ken


[Non-text portions of this message have been removed]

#26911 From: gopi chand <gopichand84@...>
Date: Sun May 30, 2010 5:23 am
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
gopichand84
Send Email Send Email
 
Hi Kenneth

This script looks a little bit better,when I run the below script the output is
below.Can you help getting rid of this erro?

OUTPUT:main::recurse() called too early to check prototype at greep.pl line 20.
Use of uninitialized value in pattern match (m//) at greep.pl line 9./

#!/usr/bin/perl -w

use strict;

sub recurse($) {
  my($path) = @_;

  ## append a trailing / if it's not there
  $path .= '/' if($path !~ /\/$/);

  ## print the directory being searched
  print $path,"\n";

  ## loop through the files contained in the directory
  for my $eachFile (glob($path.'*.zip')) {

    ## if the file is a directory
    if( -d $eachFile) {
      ## pass the directory to the routine ( recursion )
      recurse($eachFile);
    } else {

      ## print the file ... tabbed for readability
      print "\t",$eachFile,"\n";
    }
  }
}

## initial call ... $ARGV[0] is the first command line argument
recurse($ARGV[0]);




--- On Sat, 5/29/10, Kenneth Wolcott <kennethwolcott@...> wrote:

From: Kenneth Wolcott <kennethwolcott@...>
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a 
given directory
To: perl-beginner@yahoogroups.com
Date: Saturday, May 29, 2010, 9:34 PM







 









       On Sat, May 29, 2010 at 21:13, gopi chand <gopichand84@...> wrote:



>

>

>

> Hi Kenneth,

>

> Answered inline with [me]

>

>

> --- On Sat, 5/29/10, Kenneth Wolcott
<kennethwolcott@...<kennethwolcott%40gmail.com>>

> wrote:

>

> From: Kenneth Wolcott <kennethwolcott@...<kennethwolcott%40gmail.com>

> >

> Subject: Re: [PBML] Script to print all the files with .zip extentsion in a

> given directory

> To: perl-beginner@yahoogroups.com <perl-beginner%40yahoogroups.com>

> Date: Saturday, May 29, 2010, 8:54 PM

>

>

>

>

> On Sat, May 29, 2010 at 20:25, gopi chand
<gopichand84@...<gopichand84%40yahoo.com>>

> wrote:

>

> >

>

> >

>

> > I just got this over the net ,tried to run with perl grep.pl (?i)sorting

>

> > .zip but didn't work.Can you pls help?

>

> >

>

> > use strict;

>

> > use warnings;

>

> > use Cwd;

>

> >

>

> > use File::Find;

>

> >

>

> > my $search_pattern=$ARGV[0];

>

> > my $file_pattern =$ARGV[1];

>

> >

>

> > find(\&d, cwd);

>

> >

>

> > sub d {

>

> >

>

> > my $file = $File::Find::name;

>

> >

>

> > $file =~ s,/,\\,g;

>

> >

>

> > return unless -f $file;

>

> > return unless $file =~ /$file_pattern/;

>

> >

>

> > open F, $file or print "couldn't open $file\n" && return;

>

> >

>

> > while (<F>) {

>

> > if (my ($found) = m/($search_pattern)/o) {

>

> > print "found $found in $file\n";

>

> > last;

>

> > }

>

> > }

>

> >

>

> > close F;

>

> > }

>

> >

>

> Did you look at the code that you grabbed off the 'net and make any

>

> conclusions about how it works?[me]yup.I understand how it works

>

> Do you know how to use this code?[me]I don't

>

>

> Where you grabbed this code from, was there any documentation on how to

>

> use it?

>

> [me]No.

>

>

> It would help if the indentation was still in the code :-(

>

> Let's make your problem easier so that you could solve a sub-problem, then

>

> later use the knowledge gained to solve the larger problem.

>

> How would you print all of the filenames with a .zip suffix that were in

>

> the same directory using perl?[me]The below code prints all the files with

> a .zip extension but it's not doing a recursive search into subdirectories

> #!/usr/bin/perl -w

>

> @files = <*.zip>;

> foreach $file (@files) {

>   print $file . "\n";

>

> }

>

> I don't want to solve this problem *for* you; I want to *help* you solve

>

> it *yourself* :-)

>

> Ken

>



Ok, I think you solved the easier sub-problem just fine.



What is the output that you obtain when you run the script that you found

on the 'net?



I don't precisely understand how you invoked the script, perhaps there

were some unicode characters that displayed incorrectly in my gmail screen

that confused me, so show me the output that you say "didn't work".



Please be precise what you mean about "didn't work".



Ken



[Non-text portions of this message have been removed]

























[Non-text portions of this message have been removed]

#26912 From: "cincihal1" <karl.halvorson@...>
Date: Sun May 30, 2010 6:48 pm
Subject: newbie needs help with search form script
cincihal1
Send Email Send Email
 
I would like to have a search form for a glossary of terms that will be on my
website. This glossary will be on 26 pages (one page for each letter of the
Alphabet). I would like it so if the user entered in a word or words into the
search box, the result would be all the glossary terms containing those search
words arranged in order of relevance and with the search words highlighted. I
have looked at some open source cgi/perl scripts such as ksearch that are
searches for entire websites and have even tried to install this on my website
with no success. I'm not sure ksearch even does what I want on my site. Any
information on free or inexpensive Perl script that would do what I have
described and has good user support would be appreciated. In the mean time I am
going to trying to learn more about CGI/Perl.

#26913 From: Kenneth Wolcott <kennethwolcott@...>
Date: Tue Jun 1, 2010 12:53 am
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
kwolcott
Send Email Send Email
 
On Sat, May 29, 2010 at 22:23, gopi chand <gopichand84@...> wrote:

>
>
> Hi Kenneth
>
> This script looks a little bit better,when I run the below script the
> output is below.Can you help getting rid of this erro?
>
> OUTPUT:main::recurse() called too early to check prototype at greep.plline 20.
> Use of uninitialized value in pattern match (m//) at greep.pl line 9./
>
> #!/usr/bin/perl -w
>
> use strict;
>
> sub recurse($) {
>   my($path) = @_;
>
>   ## append a trailing / if it's not there
>   $path .= '/' if($path !~ /\/$/);
>
>   ## print the directory being searched
>   print $path,"\n";
>
>   ## loop through the files contained in the directory
>   for my $eachFile (glob($path.'*.zip')) {
>
>     ## if the file is a directory
>     if( -d $eachFile) {
>       ## pass the directory to the routine ( recursion )
>       recurse($eachFile);
>     } else {
>
>       ## print the file ... tabbed for readability
>       print "\t",$eachFile,"\n";
>     }
>   }
> }
>
> ## initial call ... $ARGV[0] is the first command line argument
> recurse($ARGV[0]);
>

   I see that you have chosen a different script :-)

   Here's my advice:

   There's a very old provided tool with perl called "find2perl".

   My understanding of the original problem you wish to solve is solved by
"find <dir> -name \*.zip".

   So, using "find2perl" with aforementioned find command results in this:

find2perl . -name \*.zip
#! /usr/bin/perl -w
     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
         if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '.');
exit;

sub wanted {
     /^.*\.zip\z/s
     && print("$name\n");
}

What do you think of this?

Ken


[Non-text portions of this message have been removed]

#26914 From: gopi chand <gopichand84@...>
Date: Tue Jun 1, 2010 10:49 pm
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
gopichand84
Send Email Send Email
 
Thanks a lot Kenneth.It worked.BTW where to get the find2perl tool



--- On Mon, 5/31/10, Kenneth Wolcott <kennethwolcott@...> wrote:

From: Kenneth Wolcott <kennethwolcott@...>
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a 
given directory
To: perl-beginner@yahoogroups.com
Date: Monday, May 31, 2010, 5:53 PM







 









       On Sat, May 29, 2010 at 22:23, gopi chand <gopichand84@...> wrote:



>

>

> Hi Kenneth

>

> This script looks a little bit better,when I run the below script the

> output is below.Can you help getting rid of this erro?

>

> OUTPUT:main::recurse() called too early to check prototype at greep.plline 20.

> Use of uninitialized value in pattern match (m//) at greep.pl line 9./

>

> #!/usr/bin/perl -w

>

> use strict;

>

> sub recurse($) {

>   my($path) = @_;

>

>   ## append a trailing / if it's not there

>   $path .= '/' if($path !~ /\/$/);

>

>   ## print the directory being searched

>   print $path,"\n";

>

>   ## loop through the files contained in the directory

>   for my $eachFile (glob($path.'*.zip')) {

>

>     ## if the file is a directory

>     if( -d $eachFile) {

>       ## pass the directory to the routine ( recursion )

>       recurse($eachFile);

>     } else {

>

>       ## print the file ... tabbed for readability

>       print "\t",$eachFile,"\n";

>     }

>   }

> }

>

> ## initial call ... $ARGV[0] is the first command line argument

> recurse($ARGV[0]);

>



I see that you have chosen a different script :-)



Here's my advice:



There's a very old provided tool with perl called "find2perl".



My understanding of the original problem you wish to solve is solved by

"find <dir> -name \*.zip".



So, using "find2perl" with aforementioned find command results in this:



find2perl . -name \*.zip

#! /usr/bin/perl -w

     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'

         if 0; #$running_under_some_shell



use strict;

use File::Find ();



# Set the variable $File::Find::dont_use_nlink if you're using AFS,

# since AFS cheats.



# for the convenience of &wanted calls, including -eval statements:

use vars qw/*name *dir *prune/;

*name   = *File::Find::name;

*dir    = *File::Find::dir;

*prune  = *File::Find::prune;



sub wanted;



# Traverse desired filesystems

File::Find::find({wanted => \&wanted}, '.');

exit;



sub wanted {

     /^.*\.zip\z/s

     && print("$name\n");

}



What do you think of this?



Ken



[Non-text portions of this message have been removed]

























[Non-text portions of this message have been removed]

#26915 From: merlyn@...
Date: Tue Jun 1, 2010 11:09 pm
Subject: Re: [PBML] Script to print all the files with .zip extentsion in a given directory
merlynstoneh...
Send Email Send Email
 
>>>>> "gopi" == gopi chand <gopichand84@...> writes:

gopi> Thanks a lot Kenneth.It worked.BTW where to get the find2perl tool

Same directory as Perl is in.  Probably /usr/bin or /usr/local/bin.

If Perl is in your path, so should find2perl be.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <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

#26916 From: john maclean <jayeola@...>
Date: Sun Jun 6, 2010 6:03 am
Subject: what's the source of the error here? (system command)
jayeola@...
Send Email Send Email
 
what's up with line six? The command seems fine to me :-/

cat test_dd.pl
#!/usr/bin/env perl -w
use strict;
use warnings;

my $dd_file_size = 2*10*(2**10); # 10GB
system("dd if=/dev/zero  of=/Users/jayeola/virt/running/dumb.img
seek=$dd_file_size count=1") or die "failed dd"


perl test_dd.pl
1+0 records in
1+0 records out
512 bytes transferred in 0.000033 secs (15561476 bytes/sec)
failed dd at test_dd.pl line 6.



--
John Maclean
07739 171 531
MSc (DIC)

Enterprise Linux Systems Engineer

#26917 From: merlyn@...
Date: Sun Jun 6, 2010 4:19 pm
Subject: Re: [PBML] what's the source of the error here? (system command)
merlynstoneh...
Send Email Send Email
 
>>>>> "john" == john maclean <jayeola@...> writes:

john> system("dd if=/dev/zero  of=/Users/jayeola/virt/running/dumb.img
john> seek=$dd_file_size count=1") or die "failed dd"

perldoc -f system says:

     system LIST
     system PROGRAM LIST
[...]
             The return value is the exit status of the program as returned
             by the "wait" call. To get the actual exit value, shift right by
             eight (see below).

The exit code is *zero* when everything worked OK, so in effect, you're
saying "die if everything worked"!

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <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

#26918 From: Amit Saxena <learn.tech123@...>
Date: Tue Jun 8, 2010 9:18 am
Subject: How to find the status, i.e. "next run time" and "last run time", of a task which is run through windows task scheduler !
learn.tech123@...
Send Email Send Email
 
Hi all,

I want to know how to find the status, i.e. "next run time" and "last run
time", of a task which is run through windows task scheduler.

This is required so as to find out instances where a task gets "hanged"
after run through windows task scheduler.

Thanks & Regards,
Amit Saxena


[Non-text portions of this message have been removed]

#26919 From: john maclean <jayeola@...>
Date: Tue Jun 8, 2010 1:07 pm
Subject: file stat'ing
jayeola@...
Send Email Send Email
 
Does one have to open a file before using the stat command on a file?

--
John Maclean
07739 171 531
MSc (DIC)

Enterprise Linux Systems Engineer

#26920 From: "nazanin" <nazanindarbanian@...>
Date: Tue Jun 8, 2010 6:01 pm
Subject: How to read specific information on a file
nazanindarba...
Send Email Send Email
 
Hi,
I am very new to the Perl.  And I have the following problem
I have a file which has 1000s of variables:
X1=0
Y1=1
B1=0
X2=1
….
X1000=1
I need to read this file and only collect the data which starts with X.
I s this best way to do this:
$x1 = uc('x1');
$x2=uc(`x2');
….
$x1000=uc(`x1000;);

  $fileOut = $file0 . '.txt' ;
   open (VIN, "$fileIn") ;
   open (VOUT, ">$fileOut") ;


while ( $line_in = <VIN> )
     {
	 $v1 = index ($line_in, $x1) ;
	 $v2 = index ($line_in, $x1) ;
	 ……
	 $v1000 =….
	 if ($v1 >= 0)
    	 {
	 chomp($line_in) ;
       $p2  = index ($line_in, $x1) ;
       $p3 = $p2+length($x1) ;
       $p4 = length($line_in) ;

       $varName = substr($line_in, $p3, $p4 - $p3);

	 printf VOUT "$x1 %d\n", $varName;

And write this if statement for 1000 times with new variable as x2, x3 ,…
Best Regards,
Nazanin

#26921 From: Shlomi Fish <shlomif@...>
Date: Wed Jun 9, 2010 6:27 am
Subject: Re: [PBML] file stat'ing
shlomif2
Send Email Send Email
 
On Tuesday 08 Jun 2010 16:07:34 john maclean wrote:
> Does one have to open a file before using the stat command on a file?

No, it's not necessary. stat accepts a filename as well, and it can be any
file that exists on the system (or doesn't - in which it returns the empty
list).

Regards,

	 Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

#26922 From: Shlomi Fish <shlomif@...>
Date: Wed Jun 9, 2010 6:29 am
Subject: Re: [PBML] How to read specific information on a file
shlomif2
Send Email Send Email
 
On Tuesday 08 Jun 2010 21:01:17 nazanin wrote:
> Hi,
> I am very new to the Perl.  And I have the following problem
> I have a file which has 1000s of variables:
> X1=0
> Y1=1
> B1=0
> X2=1
> ….
> X1000=1
> I need to read this file and only collect the data which starts with X.
> I s this best way to do this:
> $x1 = uc('x1');
> $x2=uc(`x2');
> ….
> $x1000=uc(`x1000;);
>
>  $fileOut = $file0 . '.txt' ;
>   open (VIN, "$fileIn") ;
>   open (VOUT, ">$fileOut") ;
>
>
> while ( $line_in = <VIN> )
>     {
>  $v1 = index ($line_in, $x1) ;
>  $v2 = index ($line_in, $x1) ;
>  ……
>  $v1000 =….
>  if ($v1 >= 0)
>      {
>  chomp($line_in) ;
>       $p2  = index ($line_in, $x1) ;
>       $p3 = $p2+length($x1) ;
>       $p4 = length($line_in) ;
>
>       $varName = substr($line_in, $p3, $p4 - $p3);
>
>  printf VOUT "$x1 %d\n", $varName;
>
> And write this if statement for 1000 times with new variable as x2, x3 ,…

No, it's not. See:

http://perl.plover.com/varvarname.html

You should use an array instead and instead of the index/substr/etc. menace,
you should probably use a regular expression. Please learn more of Perl using
http://perl-begin.org/ .

Regards,

	 Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

#26923 From: Bhavesh Vala <bhavesh2177@...>
Date: Wed Jun 9, 2010 4:16 am
Subject: Re: [PBML] file stat'ing
bhavesh_21_77
Send Email Send Email
 
Hi,


     I think there is no need to open a file before you use stat command.



>
> Does one have to open a file before using the stat command on a file?
>
> --
> John Maclean
> 07739 171 531
> MSc (DIC)
>
> Enterprise Linux Systems Engineer
>
>



--
Thanks & Regards,
Bhavesh Vala.
http://bhaveshvala.wordpress.com
________________________________


[Non-text portions of this message have been removed]

#26924 From: "mmwchang97101" <mmwchang97101@...>
Date: Wed Jun 9, 2010 7:16 pm
Subject: opening the CPAN Statistics::Descriptive package?
mmwchang97101
Send Email Send Email
 
Hi! I have some limited experience coding in Perl (mostly dealing with
spreadsheets and other delimited text files). I'm trying to using the
Statistics::Descriptive module, and I keep on getting this error message: "Can't
locate Statistics/Descriptive.pm in @INC".

I'm running the Portable Strawberry Perl (version: 5.10.1). My code is below. I
imagine there are other errors (notably in the last loop where I'm trying to
calculate the mean and standard deviation of fish counts using the functions in
the Statistics::Descriptive package).

Sorry if this message is hard to read, and I thank you in advance for your help!
I also appreciate any constructive comments about how I should order my posts in
the future.

- Charlotte

use Statistics;

open FILEIN, "<PiscoRawData.csv";
open SPECIES, "<PiscoFishCodes.csv";
open FILEOUT, ">PiscoGroupedData.csv";

print FILEOUT "Site,Side,Year,NoTransect,Group,GroupTotal,Avg,SD\n";

<SPECIES>;
while (<SPECIES>) {
	 chomp;
	 ($group,$code) = split(/,/,$_);
	 $fishgroup{$code} = $group;
}
#std dev across transects
#avg across transects
<FILEIN>;
while (<FILEIN>) {
	 chomp;
	
($school,$method,$year,$month,$day,$site,$side,$zone,$level,$transect,$fish,$cou\
nt,$length,$min,$max,$observer,$depth,$vis) = split(/,/,$_);
	 $sitetransyr = join(':',$site,$side,$transect,$year);
	 #Get number of fish total seen at each transect for each site in each year.
(Haven't figured out how to get this into summary)
	 $total{$sitetransyr} += $count;
	 #Get number of transects run by site by year
	 $notrans{$sitetransyr}++;
	 #Reclassfy fish codes into larger groups
	 $newgp = $fishgroup{$code};
	 #Combine Fish group, site, transect, and year to get the total number of that
group seen in each year
	 $fishtransyr = join(':',$site,$side,$transect,$year,$newgp);
	 $gptranstot{$fishtransyr} += $count;
	 #push(@{ $fishes{$fishtransyr} }, $count); NOT NECESSARY because variables of
interest are total across each transect, not within a transect.
}

for $fishtransyr (keys %gptranstot) {
	 #need to make a list out of the totals
	 ($site,$side,$transect,$year,$group) = split(/:/,$fishtransyr);
	 $fishyr = join(':',$site,$side,$year,$newgp);
	 $tot = $gptranstot{$fishtranstot};
	 push(@{$fishtot{$fishyr}}, $tot);
	 $sitetransyr = join(':',$site,$side,$transect,$year);
	 $transyrtot = $total{$sitetransyr};
	 $notransyr = $notrans{$sitetransyr};
	 #Reclassify the data so that I can get the number of transects run for each
site in each year
	 $siteyr = join(':',$site,$side,$year);
	 $numtranssite{$siteyr} = $notransyr;
}

for $fishyr (keys %fishtot) {
	 $gpstdev = Statistics::Sparse->new();
	 $gpavg = Statistics::Sparse->new();
	 $gpsum = Statistics::Sparse->new();
	 $gpstdev->standard_deviation(@{$fishtot{$fishyr}});
	 $gpavg->mean(@{$fishtot{$fishyr}});
	 $gpsum->sum(@{$fishtot{$fishyr}});
	 ($site,$side,$year,$group) = split(':',$fishyr);
	 $siteyr = join(':',$site,$side,$year);
	 $notransect = $numtranssite{$siteyr};
	 print FILEOUT "$site,$side,$year,$notransect,$group,$gpsum,$avg,$stdev\n";
}

#26925 From: "skchoeonearth" <skchoe@...>
Date: Fri Jun 11, 2010 12:45 am
Subject: Recursive Copy using File::Find, File::Copy
skchoeonearth
Send Email Send Email
 
Hi,
when I use find() in File::Find,
copy() always fails to find the source file w/ "No such file or directory"
message.

Here's the script:

use File::Find;
use File::Copy;

$dir = "V:/USU-BrayFilmNegatives/Batch_2-8_Original_16bit_1200ppi";

find(\&edits, $dir);

sub edits() {

     $fullpath = $File::Find::name;
     if(-e $fullpath) {
	 print "The file is found: $fullpath\n";
	 $cpresult = copy ($fullpath , "TT.tif");
	 print "COPY result = $?:$! | cpresult: $cpresult\n";
     }
     else {
	 print "The file in NOT found: $fullpath\n";
     }
}

As you see, the existence check has passed, but inside of if(),
the print says:
"COPY result = 0:No such file or directory | cpresult: 1"

What does 0 means, and what does 1 means as a value of $cpresult?

Thank you.

#26926 From: Donald Korrecta <draktrax@...>
Date: Wed Jun 16, 2010 1:25 pm
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
draktrax
Send Email Send Email
 
I'm not completely certain that copy failed.
I changed the source file to my Perl directory and the target to a file in a
documents folder.
I got one "TT.tif" file in that folder. (Can't open it but ...)
The output to STDOUT was:

The file is found: (name of file)
COPY result=0:No such file or directory | cpresult: 0

for EVERY file in the directory. Is this repetition intentional?
Are you searching for specific criteria?

still just dabbling aloooong - draktrax

--- On Thu, 6/10/10, skchoeonearth <skchoe@...> wrote:

From: skchoeonearth <skchoe@...>
Subject: [PBML] Recursive Copy using File::Find, File::Copy
To: perl-beginner@yahoogroups.com
Date:
  Thursday, June 10, 2010, 5:45 PM







 









       Hi,

when I use find() in File::Find,

copy() always fails to find the source file w/ "No such file or directory"
message.



Here's the script:



use File::Find;

use File::Copy;



$dir = "V:/USU-BrayFilmNegatives/Batch_2-8_Original_16bit_1200ppi";



find(\&edits, $dir);



sub edits() {



$fullpath = $File::Find::name;

     if(-e $fullpath) {

	 print "The file is found: $fullpath\n";

	 $cpresult = copy ($fullpath , "TT.tif");

	 print "COPY result = $?:$! | cpresult: $cpresult\n";

     }

     else {

	 print "The file in NOT found: $fullpath\n";

     }

}



As you see, the existence check has passed, but inside of if(),

the print says:

"COPY result = 0:No such file or directory | cpresult: 1"



What does 0 means, and what does 1 means as a value of $cpresult?



Thank you.

#26927 From: "skchoeonearth" <skchoe@...>
Date: Wed Jun 16, 2010 6:07 pm
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
skchoeonearth
Send Email Send Email
 
Thanks for the reply.

It's good to see that you have TT.tif in your target directory which
means the copy worked.

In my case, no file is copied to target folder. The repetition of
TT.tif is not intentional. It is just for testing of the first copy.

Basically I don't know why I get error message
"COPY result=0:No such file or directory". As long as I know, result=0 means
copy failed.

S.

--- In perl-beginner@yahoogroups.com, Donald Korrecta <draktrax@...> wrote:
>
> I'm not completely certain that copy failed.
> I changed the source file to my Perl directory and the target to a file in a
documents folder.
> I got one "TT.tif" file in that folder. (Can't open it but ...)
> The output to STDOUT was:
>
> The file is found: (name of file)
> COPY result=0:No such file or directory | cpresult: 0
>
> for EVERY file in the directory. Is this repetition intentional?
> Are you searching for specific criteria?
>
> still just dabbling aloooong - draktrax
>
> --- On Thu, 6/10/10, skchoeonearth <skchoe@...> wrote:
>
> From: skchoeonearth <skchoe@...>
> Subject: [PBML] Recursive Copy using File::Find, File::Copy
> To: perl-beginner@yahoogroups.com
> Date:
>  Thursday, June 10, 2010, 5:45 PM
>
>
>
>
>
>
>
>  
>
>
>
>
>
>
>
>
>
>       Hi,
>
> when I use find() in File::Find,
>
> copy() always fails to find the source file w/ "No such file or directory"
message.
>
>
>
> Here's the script:
>
>
>
> use File::Find;
>
> use File::Copy;
>
>
>
> $dir = "V:/USU-BrayFilmNegatives/Batch_2-8_Original_16bit_1200ppi";
>
>
>
> find(\&edits, $dir);
>
>
>
> sub edits() {
>
>
>
> $fullpath = $File::Find::name;
>
>     if(-e $fullpath) {
>
>  print "The file is found: $fullpath\n";
>
>  $cpresult = copy ($fullpath , "TT.tif");
>
>  print "COPY result = $?:$! | cpresult: $cpresult\n";
>
>     }
>
>     else {
>
>  print "The file in NOT found: $fullpath\n";
>
>     }
>
> }
>
>
>
> As you see, the existence check has passed, but inside of if(),
>
> the print says:
>
> "COPY result = 0:No such file or directory | cpresult: 1"
>
>
>
> What does 0 means, and what does 1 means as a value of $cpresult?
>
>
>
> Thank you.
>

#26928 From: Donald Korrecta <draktrax@...>
Date: Thu Jun 17, 2010 12:03 pm
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
draktrax
Send Email Send Email
 
After a little mental elbow grease I believe I have a partial answer to your
dilemma.
The question is this: Why are you copying pathnames to an image file?
I changed the second argument to copy and set it to a file created in my docs.
The  output reads:
COPY result =  0 : cpresult 1
for all but the first two files in my source and looking in the target file I
see the names
of the copied files listed.
( I put a pause after the second print so I could read the output.)
Hope this is of use to you.
Also, if the exit status is what the first half of the second print is supposed
to give,
then 0 means success.
still dabbling - draktrax
--- On Wed, 6/16/10, skchoeonearth <skchoe@...> wrote:

From: skchoeonearth <skchoe@...>
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
To: perl-beginner@yahoogroups.com
Date: Wednesday, June 16, 2010, 11:07 AM







 









       Thanks for the reply.



It's good to see that you have TT.tif in your target directory which

means the copy worked.



In my case, no file is copied to target folder. The repetition of

TT.tif is not intentional. It is just for testing of the first copy.



Basically I don't know why I get error message

"COPY result=0:No such file or directory". As long as I know, result=0 means
copy failed.



S.



--- In perl-beginner@yahoogroups.com, Donald Korrecta <draktrax@...> wrote:

>

> I'm not completely certain that copy failed.

> I changed the source file to my Perl directory and the target to a file in a
documents folder.

> I got one "TT.tif" file in that folder. (Can't open it but ...)

> The output to STDOUT was:

>

> The file is found: (name of file)

> COPY result=0:No such file or directory | cpresult: 0

>

> for EVERY file in the directory. Is this repetition intentional?

> Are you searching for specific criteria?

>

> still just dabbling aloooong - draktrax

>

> --- On Thu, 6/10/10, skchoeonearth <skchoe@...> wrote:

>

> From: skchoeonearth <skchoe@...>

> Subject: [PBML] Recursive Copy using File::Find, File::Copy

> To: perl-beginner@yahoogroups.com

> Date:

>  Thursday, June 10, 2010, 5:45 PM

>

>

>

>

>

>

>

>  

>

>

>

>

>

>

>

>

>

>       Hi,

>

> when I use find() in File::Find,

>

> copy() always fails to find the source file w/ "No such file or directory"
message.

>

>

>

> Here's the script:

>

>

>

> use File::Find;

>

> use File::Copy;

>

>

>

> $dir = "V:/USU-BrayFilmNegatives/Batch_2-8_Original_16bit_1200ppi";

>

>

>

> find(\&edits, $dir);

>

>

>

> sub edits() {

>

>

>

> $fullpath = $File::Find::name;

>

>     if(-e $fullpath) {

>

>  print "The file is found: $fullpath\n";

>

>  $cpresult = copy ($fullpath , "TT.tif");

>

>  print "COPY result = $?:$! | cpresult: $cpresult\n";

>

>     }

>

>     else {

>

>  print "The file in NOT found: $fullpath\n";

>

>     }

>

> }

>

>

>

> As you see, the existence check has passed, but inside of if(),

>

> the print says:

>

> "COPY result = 0:No such file or directory | cpresult: 1"

>

>

>

> What does 0 means, and what does 1 means as a value of $cpresult?

>

>

>

> Thank you.

>

























[Non-text portions of this message have been removed]

#26929 From: Donald Korrecta <draktrax@...>
Date: Thu Jun 17, 2010 12:26 pm
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
draktrax
Send Email Send Email
 
Actually, the output reads:
COPY result = 0  | cpresult: 1
for all but the first two and any file that has already been copied.

--- On Thu, 6/17/10, Donald Korrecta <draktrax@...> wrote:

From: Donald Korrecta <draktrax@...>
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
To: perl-beginner@yahoogroups.com
Date: Thursday, June 17, 2010, 5:03 AM







 









       After a little mental elbow grease I believe I have a partial answer to
your dilemma.

The question is this: Why are you copying pathnames to an image file?

I changed the second argument to copy and set it to a file created in my docs.

The  output reads:

COPY result =  0 : cpresult 1

for all but the first two files in my source and looking in the target file I
see the names

of the copied files listed.

( I put a pause after the second print so I could read the output.)

Hope this is of use to you.

Also, if the exit status is what the first half of the second print is supposed
to give,

then 0 means success.

still dabbling - draktrax

--- On Wed, 6/16/10, skchoeonearth <skchoe@...> wrote:



From: skchoeonearth <skchoe@...>

Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy

To: perl-beginner@yahoogroups.com

Date: Wednesday, June 16, 2010, 11:07 AM



 



Thanks for the reply.



It's good to see that you have TT.tif in your target directory which



means the copy worked.



In my case, no file is copied to target folder. The repetition of



TT.tif is not intentional. It is just for testing of the first copy.



Basically I don't know why I get error message



"COPY result=0:No such file or directory". As long as I know, result=0 means
copy failed.



S.



--- In perl-beginner@yahoogroups.com, Donald Korrecta <draktrax@...> wrote:



>



> I'm not completely certain that copy failed.



> I changed the source file to my Perl directory and the target to a file in a
documents folder.



> I got one "TT.tif" file in that folder. (Can't open it but ...)



> The output to STDOUT was:



>



> The file is found: (name of file)



> COPY result=0:No such file or directory | cpresult: 0



>



> for EVERY file in the directory. Is this repetition intentional?



> Are you searching for specific criteria?



>



> still just dabbling aloooong - draktrax



>



> --- On Thu, 6/10/10, skchoeonearth <skchoe@...> wrote:



>



> From: skchoeonearth <skchoe@...>



> Subject: [PBML] Recursive Copy using File::Find, File::Copy



> To: perl-beginner@yahoogroups.com



> Date:



>  Thursday, June 10, 2010, 5:45 PM



>



>



>



>



>



>



>



>  



>



>



>



>



>



>



>



>



>



>       Hi,



>



> when I use find() in File::Find,



>



> copy() always fails to find the source file w/ "No such file or directory"
message.



>



>



>



> Here's the script:



>



>



>



> use File::Find;



>



> use File::Copy;



>



>



>



> $dir = "V:/USU-BrayFilmNegatives/Batch_2-8_Original_16bit_1200ppi";



>



>



>



> find(\&edits, $dir);



>



>



>



> sub edits() {



>



>



>



> $fullpath = $File::Find::name;



>



>     if(-e $fullpath) {



>



>  print "The file is found: $fullpath\n";



>



>  $cpresult = copy ($fullpath , "TT.tif");



>



>  print "COPY result = $?:$! | cpresult: $cpresult\n";



>



>     }



>



>     else {



>



>  print "The file in NOT found: $fullpath\n";



>



>     }



>



> }



>



>



>



> As you see, the existence check has passed, but inside of if(),



>



> the print says:



>



> "COPY result = 0:No such file or directory | cpresult: 1"



>



>



>



> What does 0 means, and what does 1 means as a value of $cpresult?



>



>



>



> Thank you.



>



[Non-text portions of this message have been removed]

#26930 From: Donald Korrecta <draktrax@...>
Date: Sun Jun 20, 2010 5:23 pm
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
draktrax
Send Email Send Email
 
OK unless I'm completely off my noodle this is close(?) to what you're looking
for:

use File::Find;
use File::Copy;

$dir = "-> source <-"  #source file
 
find(\&edits, $dir);

sub edits()
{
    $fullpath = $File::Find::name = "-> specific file criteria <-"; #what
EXACTLY you wish to copy
    if(-e $fullpath)
    {
        print "The file is found: $fullpath\n";
        $cpresult = copy($fullpath, " -> Target <-");    # - where you
want the copy to go
        printf "COPY result = $?:$! | cpresult: $cpresult \n";
        sleep(2);  #easier to read output!
    }   
    else
    {    print "The file is NOT found: $fullpath\n";}
}

--- On Thu, 6/17/10, Donald Korrecta <draktrax@...> wrote:

From: Donald Korrecta <draktrax@...>
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
To: perl-beginner@yahoogroups.com
Date: Thursday, June 17, 2010, 5:26 AM







 









       Actually, the output reads:

COPY result = 0  | cpresult: 1

for all but the first two and any file that has already been copied.



--- On Thu, 6/17/10, Donald Korrecta <draktrax@...> wrote:



From: Donald Korrecta <draktrax@...>

Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy

To: perl-beginner@yahoogroups.com

Date: Thursday, June 17, 2010, 5:03 AM



 



After a little mental elbow grease I believe I have a partial answer to your
dilemma.



The question is this: Why are you copying pathnames to an image file?



I changed the second argument to copy and set it to a file created in my docs.



The  output reads:



COPY result =  0 : cpresult 1



for all but the first two files in my source and looking in the target file I
see the names



of the copied files listed.



( I put a pause after the second print so I could read the output.)



Hope this is of use to you.



Also, if the exit status is what the first half of the second print is supposed
to give,



then 0 means success.



still dabbling - draktrax



--- On Wed, 6/16/10, skchoeonearth <skchoe@...> wrote:



From: skchoeonearth <skchoe@...>



Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy



To: perl-beginner@yahoogroups.com



Date: Wednesday, June 16, 2010, 11:07 AM



 



Thanks for the reply.



It's good to see that you have TT.tif in your target directory which



means the copy worked.



In my case, no file is copied to target folder. The repetition of



TT.tif is not intentional. It is just for testing of the first copy.



Basically I don't know why I get error message



"COPY result=0:No such file or directory". As long as I know, result=0 means
copy failed.



S.



--- In perl-beginner@yahoogroups.com, Donald Korrecta <draktrax@...> wrote:



>



> I'm not completely certain that copy failed.



> I changed the source file to my Perl directory and the target to a file in a
documents folder.



> I got one "TT.tif" file in that folder. (Can't open it but ...)



> The output to STDOUT was:



>



> The file is found: (name of file)



> COPY result=0:No such file or directory | cpresult: 0



>



> for EVERY file in the directory. Is this repetition intentional?



> Are you searching for specific criteria?



>



> still just dabbling aloooong - draktrax



>



> --- On Thu, 6/10/10, skchoeonearth <skchoe@...> wrote:



>



> From: skchoeonearth <skchoe@...>



> Subject: [PBML] Recursive Copy using File::Find, File::Copy



> To: perl-beginner@yahoogroups.com



> Date:



>  Thursday, June 10, 2010, 5:45 PM



>



>



>



>



>



>



>



>  



>



>



>



>



>



>



>



>



>



>       Hi,



>



> when I use find() in File::Find,



>



> copy() always fails to find the source file w/ "No such file or directory"
message.



>



>



>



> Here's the script:



>



>



>



> use File::Find;



>



> use File::Copy;



>



>



>



> $dir = "V:/USU-BrayFilmNegatives/Batch_2-8_Original_16bit_1200ppi";



>



>



>



> find(\&edits, $dir);



>



>



>



> sub edits() {



>



>



>



> $fullpath = $File::Find::name;



>



>     if(-e $fullpath) {



>



>  print "The file is found: $fullpath\n";



>



>  $cpresult = copy ($fullpath , "TT.tif");



>



>  print "COPY result = $?:$! | cpresult: $cpresult\n";



>



>     }



>



>     else {



>



>  print "The file in NOT found: $fullpath\n";



>



>     }



>



> }



>



>



>



> As you see, the existence check has passed, but inside of if(),



>



> the print says:



>



> "COPY result = 0:No such file or directory | cpresult: 1"



>



>



>



> What does 0 means, and what does 1 means as a value of $cpresult?



>



>



>



> Thank you.



>



[Non-text portions of this message have been removed]

























[Non-text portions of this message have been removed]

#26933 From: "Jenda Krynicky" <Jenda@...>
Date: Thu Jun 24, 2010 4:51 pm
Subject: Re: [PBML] Recursive Copy using File::Find, File::Copy
jendaperl
Send Email Send Email
 
From: "skchoeonearth" <skchoe@...>
> Hi,
> when I use find() in File::Find,
> copy() always fails to find the source file w/ "No such file or directory"
message.

Unless this is meant as an exercise you should simply use
File::Copy::Recursive (portable) or Win32::FileOp (Windows only).

Jenda
===== Jenda@... === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
	 -- Terry Pratchett in Sourcery

#26934 From: "literatecat" <literatecat@...>
Date: Mon Jun 28, 2010 2:18 am
Subject: undefined subroutine
literatecat
Send Email Send Email
 
Hello all,

I am trying to run some scripts from extropia and getting an "undefined
subroutine" error during setup.  Can anyone help with what I should be looking
for to get through this error?

Cat

#26935 From: Yanto asnawi <yanto_asnawi@...>
Date: Mon Jun 28, 2010 7:38 am
Subject: Resize jpg image
yanto_asnawi
Send Email Send Email
 
Hello All,

Please help me,
I need to resize image jpg file (from upload, any size) into some size ex. 640 x
480 pixel.

Do anyone know how to do?

Thanks alot,
Yanto


[Non-text portions of this message have been removed]

#26936 From: Jonas Bull <me@...>
Date: Mon Jun 28, 2010 1:13 pm
Subject: Re: [PBML] Resize jpg image
me@...
Send Email Send Email
 
My non-expert suggestion:
http://www.imagemagick.org/script/perl-magick.php

On 6/28/10, Yanto asnawi <yanto_asnawi@...> wrote:
> Hello All,
>
> Please help me,
> I need to resize image jpg file (from upload, any size) into some size ex.
> 640 x 480 pixel.
>
> Do anyone know how to do?
>
> Thanks alot,
> Yanto
>
>
> [Non-text portions of this message have been removed]
>
>


--

Jonas Bull
601-324-0324 (Office)
228-222-2855 (Home)

#26937 From: "khema1808" <khema1808@...>
Date: Wed Jun 30, 2010 11:38 pm
Subject: how to store a formatted output to an array/hash and unformat the data?
khema1808
Send Email Send Email
 
I have to process data which is in the form of rows and columns.  How do I store
each row into an array, split the contents of the array with a delimiter?

eg: this is the output:

x.x.x.x        I    1,400,000             0                  0                 
0                0               0            0  25,000,000         0           
0              0        2730        1         0  0.0.0.0            0          
0            0              0          0

can anyone give me ideas/suggestions? i'm perl beginner.

Thanks

#26938 From: "sxsakura" <sxsakura@...>
Date: Thu Jul 1, 2010 11:14 am
Subject: Need help with Hash Values
sxsakura
Send Email Send Email
 
Em....While doing the homework that gives from the "Learning Perl" books.
The homework is "Give out some strings (a word per line) and count the word that
has been given"
so I typed:
#! /usr/bin/perl -w

while(<>)
{
     <STDIN>;
     chomp;
     $words{$_} += 1;
}

foreach (sort keys %words)
{
     print "$_ has appeared $words{$_} times\n";
}

and run it,and these are what i typed and what it gave back:
hello
hello
hi
hi
nihao
nihao
hey
hei
hello has appeared 1 times
hey has appeared 1 times
hi has appeared 1 times
nihao has appeared 1 times

Obviously,the words"hello","hi","nihao" has appeared 2 times,but the program
only counted 1 time,why?
And if I use a array of values to read the input data and then count it,it will
goes well
like:
@word=<STDIN>;
foreach (@word)
{
   $counts{$_} += 1;
}
why~?
need help,thanks

#26939 From: Shlomi Fish <shlomif@...>
Date: Thu Jul 1, 2010 11:29 am
Subject: Re: [PBML] Need help with Hash Values
shlomif2
Send Email Send Email
 
On Thursday 01 Jul 2010 14:14:07 sxsakura wrote:
> Em....While doing the homework that gives from the "Learning Perl" books.
> The homework is "Give out some strings (a word per line) and count the word
> that has been given" so I typed:
> #! /usr/bin/perl -w
>
> while(<>)
> {
>     <STDIN>;

You don't need this statement (the "<STDIN>;" one). It just skips a line.
Without it, it should be fine.

Regards,

	 Shlomi Fish

>     chomp;
>     $words{$_} += 1;
> }
>
> foreach (sort keys %words)
> {
>     print "$_ has appeared $words{$_} times\n";
> }
>
> and run it,and these are what i typed and what it gave back:
> hello
> hello
> hi
> hi
> nihao
> nihao
> hey
> hei
> hello has appeared 1 times
> hey has appeared 1 times
> hi has appeared 1 times
> nihao has appeared 1 times
>
> Obviously,the words"hello","hi","nihao" has appeared 2 times,but the
> program only counted 1 time,why? And if I use a array of values to read
> the input data and then count it,it will goes well like:
> @word=<STDIN>;
> foreach (@word)
> {
>   $counts{$_} += 1;
> }
> why~?
> need help,thanks

--
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

#26940 From: "sxsakura" <sxsakura@...>
Date: Thu Jul 1, 2010 11:25 am
Subject: Need help with Hash Values
sxsakura
Send Email Send Email
 
I try to save some values,and count the words that has given by user.
and write this:
#! /usr/bin/perl -w
while(<>)
{
     <STDIN>;
     chomp;
     $words{$_} += 1;
}

foreach (sort keys %words)
{
     print "$_ has appeared $words{$_} times\n";
}

But when type these:
hello
hello
hi
hi
nihao
nihao
hey
hei

It returns:
hello has appeared 1 times
hey has appeared 1 times
hi has appeared 1 times
nihao has appeared 1 times

Obviously,some words like "hello",appears 2 times,but the program only counted 1
time.

But,if write like this:
#! /usr/bin/perl -w
@words=<STDIN>;
foreach (@words)
{
   $counts{$_} += 1;
}
foreach (sort keys %counts)
{
   print "$_ appeared $counts{$_} times\n";
}

It will works quite well.
Do anyone know why I can't save the @word value?

Messages 26909 - 26940 of 27459   Oldest  |  < Older  |  Newer >  |  Newest
Add to My Yahoo!      XML What's This?

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help