KevinADC 192 Practically a Posting Shark

WWW::Mechanize does not come with perl it has to be installed. You can see a listing of all perls core modules here:

http://perldoc.perl.org/index-modules-A.html

KevinADC 192 Practically a Posting Shark

There is no standard way that I am aware of. It really makes no sense to search a bin file for "words" but maybe for school work it does.

KevinADC 192 Practically a Posting Shark

The google search found many many examples, so I assume you didn't search, and I assume you don't want anymore help from me. I'll just take my smart ass over to another thread and help people that aren't hyper sensitive.

KevinADC 192 Practically a Posting Shark

You need to use a file test operator -M or the stat() function to get the date and the sort() function to sort them. But I bet a Google search would find plenty of examples of code already written, so search yourself:

http://www.google.com/search?q=perl+sort+files+by+date

KevinADC 192 Practically a Posting Shark

considering that a .bin file is a compressed and encoded file its no wonder you can't just open it like a text file.

KevinADC 192 Practically a Posting Shark

If you write the regexp like this:

/Check 8/

it will match that substring inside of any larger string, so "Check 8000" would match, but not "Check 1008" it would also match "PreCheck 811181118" as long as the regexp find "Check 8" anywhere in the string in that exact order it will return true. You may need to use some type of anchor to force the match to match only at specific parts of a string, the beginning or end for example. But on another note "Check 8" is not a pattern its a string or a substring so using index() might be more appropriate.

KevinADC 192 Practically a Posting Shark

It will match. I just posted code that proves that. But your correct, it works fine without the // delimiters.

Are you using chomp() on the user input before using it in the regexp?

KevinADC 192 Practically a Posting Shark

Well, the first thing is to properly construct your regexp:

if($data =~ /$user_input/){then do something;}

assuming $data is the number like 1008 and 800, etc, and $user_input is the number 8 then it would match any string that has the number 8 in it anywhere. You can easily test that:

@nums = qw(800 1008 34 8 99 111118  11111 8111111 111181111);
$user_input = 8;

foreach my $data (@nums) {
   if($data =~ /$user_input/){print "Matched $data\n";}
}

output:

Matched 800
Matched 1008
Matched 8
Matched 111118
Matched 8111111
Matched 111181111

KevinADC 192 Practically a Posting Shark

There is no pearl language but there is perl. Is it the easiest? No. There are plenty of perl books, you want "Learning Perl" the newest edition. Search for it on amazon.com

The easiest language to learn might be BASIC, but I have no idea what you would use it for.

KevinADC 192 Practically a Posting Shark

Well the 'my' exists inside a { } scope, so I guess it goes out of scope when the block exits.

Yep, thats probably the reason.

my @contents = ();
if (-e $path_to_file)
{
open (FILE, $path_to_file) or die "$!";
@contents=<FILE>;
close(FILE);
}
print "$_\n" for @contents;
KevinADC 192 Practically a Posting Shark

We are adding a newline to the output when the lines of the file already have a newline at the end so we create double-spacing for any line that already has a new line. Sometimes the last line of a file does not have a newline on the end so if you print it in reverse order the first and second lines (of the reversed order) appear to be on the same line. The safest thing to do is use chomp() and print the newline during the output.

open(LISTFILE, $filename);
@filearray = <LISTFILE>;
chomp @filearray;
close LISTFILE;
print "$_\n" for reverse @filearray;
\
KevinADC 192 Practically a Posting Shark

OK, now the easy way......

open(LISTFILE, $filename);
@filearray = <LISTFILE>;
close LISTFILE;
print "$_\n" for reverse @filearray;
KevinADC 192 Practically a Posting Shark

At some point you should start doing your own school work and the rest of the people participating in this thread you stop doing it for you.

KevinADC 192 Practically a Posting Shark

How to print the duplicates in a text file. Also the line number where the duplicate text has been found needs to be printed. pls help

KZ

What have you tried? Where are you stuck?

KevinADC 192 Practically a Posting Shark

You need to clarify your requirements better. Right now you are looping through num.txt and populating a hash with the lines of the file and the count of how many times each line is seen in the file. From there what do you want to do? See if the same line is in a different file? Otherwise your code makes no sense because you open the same file and then look to see if the lines are in the same file, which of course they are.

KevinADC 192 Practically a Posting Shark
KevinADC 192 Practically a Posting Shark

Sorry, I have read your question three times and I can't understand what you want to do.

KevinADC 192 Practically a Posting Shark

Wrong forum mate

KevinADC 192 Practically a Posting Shark

But it seems all the comments is so far had not helped me to add any new thin to my code.

Your code? You didn't write one single thing in that code, you just copied and pasted it from the other forum. You really have no shame.

KevinADC 192 Practically a Posting Shark

Why are you not asking your SkillsTraining Tutor for help? You are paying them after all.

KevinADC 192 Practically a Posting Shark

I am surprised that you got this program to run at all.

Considering they did not write it, and probably never even ran it, your surprise should be alleviated.

KevinADC 192 Practically a Posting Shark

Akase...

You insult everyone helping on this forum by posting your plagarized code and making it appear as if you wrote it.

http://www.programmersheaven.com/mb/perl/372885/392147/re-analysing-text-files-to-obtain-statistics-on-their-content/?S=B20000#392147

KevinADC 192 Practically a Posting Shark

click on the "flag bad post" link in the threads you want deleted and ask that they be deleted. Provide a good reason though.

KevinADC 192 Practically a Posting Shark

The accepted protocol is that first you try and solve your own programming requirements and post the code you wrote. Then when you are stuck you ask a question. But what is not acceptable is just asking people to do your work (school or otherwsie) for you.

Asking a question like you did above without any explnation will lead to a contrived solution that will only work for the sample data you post. You need to describe as best as possible in non-programming terms why the program should find the lines you underlined.

KevinADC 192 Practically a Posting Shark

You can run your perl scripts from the command prompt as ithelp is suggesting, but it is much easier to use a perl IDE to write, edit, and run your perl programs. A free one for Windows is Perl-Express:

http://www.perl-express.com/

There are other free ones and you can also purchase a perl IDE. I have used PerlBuilder2 Pro for years but there are even better ones for more money. For now stick with Perl-Express and if you need something better you can always get it later.

KevinADC 192 Practically a Posting Shark

$name is being used for no good reason that I can see:

open (INP, "input.txt") or die "Can't open input: $!\n";
open (OUT, ">>output.txt") or die "Can't open output: $!\n";
while ($line = <INP>) {
   chomp ($line);
   @field = split (/\s+/, $line);
   print OUT "($field[0])\t$field[1]\n";
}
close INP;
close OUT;

No need to make a new string just to print to the file. I added \n to the end of the line, remove it if you don't want a newline on the end.

KevinADC 192 Practically a Posting Shark

Two easy choices

activeperl from http://www.activestate.com

or

strawberryperl from http://www.strawberryperl.com

they come with installation instructions.

KevinADC 192 Practically a Posting Shark
KevinADC 192 Practically a Posting Shark
KevinADC 192 Practically a Posting Shark

Here is an article I wrote for another IT forum, maybe it will help you:

http://bytes.com/topic/perl/insights/672398-how-upload-files-using-cgi-pm-module-perl

KevinADC 192 Practically a Posting Shark

Using "strict" is important , especially for new perl coders, but it is not necessary to use it. But you will see that 99% of all perl scripts use "strict" and most use "warnings". The -T switch (not -t) is also important for a CGI script. It is there to help prevent something dangerous from occuring, like using user input to open a file.

To lock a file you use the flock() function:

http://perldoc.perl.org/functions/flock.html

KevinADC 192 Practically a Posting Shark

The perl documentation would have explained all of your errors. But I'll short-cut it for you.

First, $a and $b are variables used by perl for sorting and should not be used by you except for that purpose. Since they are global package variables you don't have to declare them with "my".

Quoted from the sort functions documentation:

In the interests of efficiency the normal calling code for subroutines is bypassed, with the following effects: the subroutine may not be a recursive subroutine, and the two elements to be compared are passed into the subroutine not via @_ but as the package global variables $a and $b (see example below). They are passed by reference, so don't modify $a and $b . And don't try to declare them as lexicals either.

So $a and $b need to be changed and all your variable need to be declared within scope using "my".

#!/usr/bin/perl
use warnings; 
use strict;

my $x = 6;
my $y = 9;
my $c = 7;

print $x, "\n";
print $y, "\n";
print $c, "\n";

I changed "." to "," in the print lines because using the dot (concatenation) is slow. 'print' is a list operator so it expects a list of things to print, so using a comma to create a list of thing to print is very fast as it doesn't force perl to build any intermitent strings first.

Quoted from the print operators documentation:

Prints a string …

VernonDozier commented: Helpful. +15
katharnakh commented: Brilliant explanation to know something unknown :) +4
KevinADC 192 Practically a Posting Shark

Yes there is something you can do. There are a few things actually. But I think this is the best suggestion. Instead of returning a list of scalars with some that you don't use, return a list slice consisting only of the ones you want to use. Look at this line:

($tax_id,$geneID,$genesymbol,$gene_des,$proteinacc_ver,$mrna_acc,$length,$nucleotide_gi,$start_gene,$end_gene,$strand) = split("\t");

It returns a list of 11 scalars. They are numbered 0 thru 10, so your line above is the same as this:

($tax_id,$geneID,$genesymbol,$gene_des,$proteinacc_ver,$mrna_acc,$length,$nucleotide_gi,$start_gene,$end_gene,$strand) = (split("\t"))[0..10];

Now what you can do is take just the elements of the list you want by including only those ones in the index list, say you only want $geneId (#1) and $nucleotide_gi (#7):

my ($geneID,$nucleotide_gi) = (split("\t"))[1,7];

Note the extra set of parenthisis on the right side of the assignment operator, that is important. Note I added "my" in the above line also. You need to start using "strict" and "warnings" with your script (not the -w switch) and declaring the varaibles within their intended scope of use with "my" (lexical) or "our" (global). You can look them all up in the perl documentation:

strict
warnings
my
our

KevinADC 192 Practically a Posting Shark

Trust me, this is the best place to get help. Especially for such a simple problem.

Daniweb is a great forum, but for perl questions, www.perlmonks.com is considerably better. Also:

experts-exchange.com
tek-tips.com
perlguru.com
devshed.com

all have good perl forums, much more active than Daniwebs perl forum. But I really like Daniweb, which has one of the best webmasters (or webmistress) of any forum I have ever been a member of, and I have been a member of a lot of programming/IT forums.

KevinADC 192 Practically a Posting Shark

does anybody know a good forum to go to. I know that daniweb is good for java and c++

thanks!

Whats wrong with this forum? You didn't get a reply fast enough? This is a help forum with volunteer members, not tech support. Be patient.

KevinADC 192 Practically a Posting Shark

This line also needs to be changed:

$nucleotide_gi,$start_gene,$end_gene,$strand) = split('\t');

\t in single-quotes is a literal string, not a tab, so use forward slashes for the split() delimter:

$nucleotide_gi,$start_gene,$end_gene,$strand) = split(/\t/);
KevinADC 192 Practically a Posting Shark

I didn't look at all your code but this line:

$content = get('http://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?db=protein&sendto=t&dopt=fasta&list_uids=$protacc_arr[$i]');

Needs double-quotes so that the variables are interpolated:

$content = get("http://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?db=protein&sendto=t&dopt=fasta&list_uids=$protacc_arr[$i]");

Change that and see if it helps.

KevinADC 192 Practically a Posting Shark

Try perlmonks.com if you get no replies here. I can't help with your question.

KevinADC 192 Practically a Posting Shark

You should be able to do both in one header because the cookie is not a content-type entity. I am not real experienced with cookies but maybe if you post your code I can help, or maybe someone else can.

KevinADC 192 Practically a Posting Shark

The code you posted will work to append the string to the end of whatever value $values[10] has. If its not working the problem is elsewhere.

KevinADC 192 Practically a Posting Shark

my (@m) = $s =~ m/<meta (.*?)>/gis;

You can look up the regexp modifiers in any regexp tutorial.

i - case insentive matching
s - match as a single string so matches across newlines
g - global match, works like grep, finds all matches in a string/line

KevinADC 192 Practically a Posting Shark

try:

my ($d) = $s =~ m/<head>(.*?)<\/head>/is;

KevinADC 192 Practically a Posting Shark

The question is probably best asked on a forum dedicated to http servers and not a CGI forum dedicated to CGI scripts.

KevinADC 192 Practically a Posting Shark

"do" is not really a loop but if you use "until" or "while" with it you can get it to act like a loop. The problem with your code is that $start never has a chance to equal zero so the "do" block just keeps getting repeated over and over. Thix will help you see whats going on:

use warnings;
use strict;
my @list;
print "Enter number of OTUs:";
my $start = <STDIN>;
chomp $start;
do {
my $rand = int(rand($start));
print "rand = $rand\n";
push (@list, $rand);
$start-=$rand;
print "\$start = $start\n";
} until ($start == 0);

print "@list";
KevinADC 192 Practically a Posting Shark

I am not familiar with the useradd application, maybe you need to provide a path to the application instead of just using the application name.

KevinADC 192 Practically a Posting Shark

if useradd is an external application you probably want to use sytemt() to run it:

#!/usr/bin/perl

$uName=$ARGV[0];
$uNumStart=$ARGV[1];
$uNumEnd=$ARGV[2];

for($i=$uNumStart; $i<=uNumEnd; $i++)
{
   system(qq{useradd "$uName.$i" -g 999});
}
KevinADC 192 Practically a Posting Shark

If you really want to do it manually you need a recursive function, something like this:

use warnings;
use strict;

#push @INC, "C:/Perl/Modules";

print "What is current download directory? ";
my $dir = <STDIN>;
chomp $dir;

list($dir);

sub list {
   my ($dir) = @_;
   print "$dir\n";
   opendir(my $DH, $dir) or do{print "Can't open $dir: $!\n"; return};
   my @list = grep {$_ ne '.' and $_ ne '..'} readdir $DH;
   foreach my $file (@list){
      print "\t$file\n";
      list("$dir/$file") if (-d "$dir/$file");
   }
}
musturd commented: Helped a lot! +2
KevinADC 192 Practically a Posting Shark

If you want to drill down through all sub directories you should use the File::Find module that comes with perl.

KevinADC 192 Practically a Posting Shark

Work on the script one problem at a time. First check that the directory I/O is working:

use warnings;
use strict;

push @INC, "C:/Perl/Modules";

print "What is current download directory? ";
my $dir = <STDIN>;
chomp $dir;
print "Current Directory is: $dir\n\n";
opendir DH, $dir or die "Can't open directory: $!";
while (my $parent = readdir(DH)){
   next if ($parent eq "." or $parent eq "..");
   print "> $parent\n";
   if(-d $parent){
      opendir DI, "$dir/$parent" or die "Can't open directory: $!";
      while (my $sub = readdir(DI)){
         next if ($sub eq "." or $sub eq "..");
         print ">>> $sub\n";
      }
      close DI;
   }
}
close DH;

Then if that is working try adding in the regexp stuff and see what happens. If it does not work then you have to figure out what the problem is.

KevinADC 192 Practically a Posting Shark

Try this, I didn't really fix any syntax errors though besides "else if":

#!/usr/bin/perl
use warnings;
use strict;

push @INC, "C:/Perl/Modules";

print "What is current download directory? ";
my $dir = <STDIN>;
chomp $dir;
opendir DH, $dir or die "Can't open directory: $!";
while (my $parent = readdir(DH))
{
	next if ($parent eq "." or $parent eq "..");
	if(-d $parent)
	{
		opendir DI, "$dir/$parent" or die "Can't open directory: $!";
		while ($_ = readdir(DI))
		{
			next if ($_ eq "." or $_ eq "..");
			if (/(\w+ )(\d+)\b(\d+)\b(.*)(\d+)/)
			{
				print "Long File";
				our $month = $1;
				our $day = $2;
				our $year = $3;
				our $name = $5;
			}
			elsif(/(\w+ )(\d+)\b(\d+)\b(.*)(\d)\b/)
			{
				print "Short File";
				our $month = $1;
				our $day = $2;
				our $year = $3;
				our $name = $5;
			}
		}
	}
}
print "Done";