KevinADC 192 Practically a Posting Shark

It aborts because you can't have nested quantifiers.

Your expectation of what the "g" modifier does is also wrong. Here is an excerpt from perlretut:

Global matching

The final two modifiers //g and //c concern multiple matches. The modifier //g stands for global matching and allows the matching operator to match within a string as many times as possible. In scalar context, successive invocations against a string will have `//g jump from match to match, keeping track of position in the string as it goes along. You can get or set the position with the pos() function.

The use of //g is shown in the following example. Suppose we have a string that consists of words separated by spaces. If we know how many words there are in advance, we could extract the words using groupings:

1. $x = "cat dog house"; # 3 words
2. $x =~ /^\s*(\w+)\s+(\w+)\s+(\w+)\s*$/; # matches,
3. # $1 = 'cat'
4. # $2 = 'dog'
5. # $3 = 'house'

But what if we had an indeterminate number of words? This is the sort of task //g was made for. To extract all words, form the simple regexp (\w+) and loop over all matches with /(\w+)/g :

1. while ($x =~ /(\w+)/g) {
2. print "Word is $1, ends at position ", pos $x, "\n";
3. }

prints

1. Word is cat, ends at position 3
2. Word …

KevinADC 192 Practically a Posting Shark

You need parentheses for back references to capture patterns they find.


if (/($pattern)/) {

KevinADC 192 Practically a Posting Shark

hashes are not randomly ordered lists, they are lists that don't have any guaranteed ordered, but they should always be the same order on the same computer. So that bug report appears to be erroneous because the reporter did not seem to understand that hashes are not randomly ordered.

If you need to use a hash, you can store the keys in an array and use List::Util and the shuffle function to randomly order the array with the hash keys. Then loop over the array instead of the hash. An example:

use List::Util qw/shuffle/;
my @keys = (1..26);
my @values = ('a'..'z');
my %hash;
@hash{@keys}=@values;

@keys = shuffle(@keys);
for(@keys){
    print "$hash{$_}\n";
}
KevinADC 192 Practically a Posting Shark

What are you entering into $pattern?

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

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

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

try:

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

KevinADC 192 Practically a Posting Shark
$decVal = <STDIN>;
chomp($decVal);#<--- you need this
KevinADC 192 Practically a Posting Shark

use split instead:

my $var = 'abcdefghijklmnop';
my @chars = split(//,$var);
foreach my $char (@chars) {
   #do something with $char
}
KevinADC 192 Practically a Posting Shark

CGI::Pretty comes with perl. I leave it up to you to read the documentation.

KevinADC 192 Practically a Posting Shark

Ok, I got it to work:

print $cgi->start_html(-title => 'Siddharth\'s Search Engine', -head => "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no cache\" />"), "\n";

Although, I would still like it more if there was a better way to do it.

Its covered in the CGI modules documentation, you were pretty close actually:

use CGI;
my $cgi = CGI->new;
print $cgi->start_html(-title => 'Meta Test',
                       -head=>$cgi->meta({-http_equiv => 'Pragma',
                                          -content    => 'no-cache'}));
KevinADC 192 Practically a Posting Shark

I don't know, you can try www.perlmonks.com or ask on the dbi modules mailing list (see the DBI module for details)

KevinADC 192 Practically a Posting Shark
#!/usr/local/bin/perl
use English;

$string = "This is my string this is it.  I want to tie my string";

$count = 0;

while ($string =~ m/string/gi)
{
  $count=$count+1;
}

$string =~ /string/; 
print "$count strings\n";
print "\n Match: $MATCH\n";
print "\n PreMatch: $PREMATCH\n";
print "\n PostMatch: $POSTMATCH";
KevinADC 192 Practically a Posting Shark

If you literally want to find "string" you can't put it in square brackets. That makes a character class and order of the characters is ignored.

KevinADC 192 Practically a Posting Shark

use the "g" option:

$string = 'abc 123 def 456 ghi 789';
@numbers = $string =~ /\d+/g;
print "$_\n" for @numbers;

Note I changed the quantifier * to + because * will match zero or more digits so it will match anything that is not a digit although it will just be an empty match.

That should be covered in any regexp tutorial

KevinADC 192 Practically a Posting Shark

That's not how rand works:

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

Hes just not applying it correctly to this particular problem.

KevinADC 192 Practically a Posting Shark

The problem is you are generating a random number based on the length of the array, not the values of the elements of the array. Here is what you want to do:

my @numbers = (48..57, 65..90, 97..112);
my $num = $numbers[int rand @numbers];
print $num,"\n";
KevinADC 192 Practically a Posting Shark
if($form_element{'email'} =~ m/^[a-zA-Z0-9_\-]+@[a-zA-Z0-9_\-]+([.][a-zA-Z0-9_\-]+)+[a-zA-Z]{2,4}$/i){
        $values = 0;
        $mail = "Valid!";
} else {
        $mail = "InValid!";
}
sid78669 commented: Excellently answered!! +1
KevinADC 192 Practically a Posting Shark

just a guess, in this block of code:

#open (MLB2, ">> /data/local/GIS/WRKGIS.txt");
open (MLB2, ">> /awips/dev/localapps/kmltools/WRKGIS1.csv");
for ($i=1; $i<=$num1; ++$i){
 if ($finline[$i] =~/[A-Z]+/ ){
 write(MLB2);
format MLB2=
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$finline[$i];
.
 }
}

change $i<=$num1 to $i<$num1

KevinADC 192 Practically a Posting Shark

see the other forum, I prefer to not duplicate my replies.

KevinADC 192 Practically a Posting Shark

I see you posted the same question on another forum and provided more detail. My suggestion above will not do what you want.

KevinADC 192 Practically a Posting Shark

csv files typically use quotes around fields that have text or non-digit data in them. If you don't want the quotes you can try this:

print (join ',' , "@dn", # row
map {get $entry, $_->[1], $_->[2]} @fields)."\n";
}
KevinADC 192 Practically a Posting Shark

PPM is not for running your perl programs. Its for installing modules and other tasks related to modules. On Windows the way I prefer to run a perl program is open a DOS (or Command) Window and type:

perl scriptname optional-arguments

You may need to add the path to the perl program:

perl c:\whatever\scriptname optional-arguments

activeperl also comes with a whole lot of documentation so you need to start reading it.

KevinADC 192 Practically a Posting Shark

If you tried to run your code it will return a syntax error so I am suspicious you did not try it. In the other code the "while" loop has a condition:

while (1) {

why did you remove the condition in parenthesis?

KevinADC 192 Practically a Posting Shark

A students best cheat tool is a search engine or the search feature of a website or forum. You would have found a recent thread discussing the exact same assignment had you done just a little searching.

http://www.daniweb.com/forums/thread169597.html

Salem commented: I like it :) +27
KevinADC 192 Practically a Posting Shark

The Benchmark documentation says:

CAVEATS

Comparing eval'd strings with code references will give you inaccurate results: a code reference will show a slightly slower execution time than the equivalent eval'd string.

The real time timing is done using time(2) and the granularity is therefore only one second.

Short tests may produce negative figures because perl can appear to take longer to execute the empty loop than a short test; try:

timethis(100,'1');

The system time of the null loop might be slightly more than the system time of the loop with the actual code and therefore the difference might end up being < 0.

KevinADC 192 Practically a Posting Shark

You can try the Benchmark or Time::HiRes modules, they come with perl. Read the documentation and try a few things, if you get stuck post back.

KevinADC 192 Practically a Posting Shark

Here is a simple test of the regexp I posted:

$row = [qq{this is\n a test},qq{this is\ranother test}];
s/[\r\n]// for @{$row};
print @{$row};

That appears to work fine. Why it causes your code to seemingly hang, I don't know. In the code you posted before, do this:

my $row = $csv->getline ($fh);
print $row,"\n";

and see what $row is. If it does not return something like ARRAY(0x155526c) then what does it print?

KevinADC 192 Practically a Posting Shark

this line is doing nothing (its useless):

($ch);

I am not sure what you think this does:

open(OUT, "<$file") || die "Cant open $file: $!";
for($line=0; <OUT>; $line++)
{ 
}

I'm surprised that doesn't return a syntax error, but it definetly isn't doing anything and that is where $line is used only once. If you want to count the lines in the file:

open(OUT, "<$file") || die "Cant open $file: $!";
$line++ while (<OUT>);
KevinADC 192 Practically a Posting Shark

Post you code again, the code that returns the warning you mention. "Useless use of variable" is generally something like:

$foo;

The above line does nothing, its useless. Thats a warning though, not an error. And the other warning is obvious, you have something used only once in your program (a sclar or filename or filehandle, etc) , that is also a warning.

KevinADC 192 Practically a Posting Shark

Well, I don't know what the character is that is causing the problem but it appears you will need to remove it manually, something like:

my $row = $csv->getline ($fh);
s/[\r\n]// for @{$row};

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

Your code is quite close to getting it right, here is a way to do this type of looping guessing game:

use strict;
use warnings;

my $thinkingof = int(rand 10)+1;
while (1) {
   print "Pick a number 1 to 10\n";
   my $guess = <STDIN>;
   chomp $guess;
   if ($guess > $thinkingof){
      print "You guessed too high\n";
      next;
   }
   elsif ($guess < $thinkingof){
      print "You guessed too low\n";
      next;
   }
   print "YOU GUESSED RIGHT!! It was $guess\n";
   last;
}
KevinADC 192 Practically a Posting Shark

inked,

add this line to your code:

use warnings;

and rerun it and list any warnings you get.

KevinADC 192 Practically a Posting Shark

A google search found this page:

http://en.literateprograms.org/Category:Programming_language:Perl

It shows a "word count" program listed but I have not read it to see how well written it is.

And this one:

http://www.perlmonks.org/?node_id=457784

KevinADC 192 Practically a Posting Shark

The concept of sentences does not translate well to poetry or song liricks as they do not follow the same grammatical rules as formal writing.

KevinADC 192 Practically a Posting Shark

OK, when I run your code the count is 2. The problem appears to be that you are not chomping the input and you have "!" instead of "," in your conditional statement.

When "!" is changed to "," the count goes to 7 because the first sentence has 2 commas:

For the eyeing of my scars, there is a charge,

You also need to chomp $ch:

chomp($ch)

before comparing the strings.:

while(my $ch = getc(DATA))
{
chomp($ch);
if($ch eq "?" || $ch eq "," || $ch eq ".")
{
$sentences++;
}
}
KevinADC 192 Practically a Posting Shark

You are comparing strings:

if($ch eq"?" || $ch eq "!" || $ch eq ".")

instead of searching for patterns:

if($ch =~ /[?!.]/)

EDIT: actually, let me try something, I think getc() might be affecting the code too. I never use it so I have to check how it works.

KevinADC 192 Practically a Posting Shark

Your latest code no longer has the end of string anchor ($) so it could match strings of any length as long as the initial pattern matches. But lets assume your code does have the end of string anchor and run a test:

while (my $file = <DATA>) {
   chomp($file);
   if( $file !~ m/^[a-zA-Z\_]{1}[a-zA-Z0-9\_]{7}(\.txt|\.TXT)$/){
      print "$file - Incorrect format!\n";  
   }
   else {
      print "$file - correct format!\n";
	}	
}
__DATA__
qwertyuio.txt
12wedcfr.TXT
asd38_gy.TxT
asd38_gy.txt
@qwedcvnt.txt
asdfgter.txt
dgh!cbgt.txt
1234rtyuio.TXT
a123dfghj.txt
aqwertyu.gif

output:

qwertyuio.txt - Incorrect format!
12wedcfr.TXT - Incorrect format!
asd38_gy.TxT - Incorrect format!
asd38_gy.txt - correct format!
@qwedcvnt.txt - Incorrect format!
asdfgter.txt - correct format!
dgh!cbgt.txt - Incorrect format!
1234rtyuio.TXT - Incorrect format!
a123dfghj.txt - Incorrect format!
aqwertyu.gif - Incorrect format!

looks good to me.

KevinADC 192 Practically a Posting Shark

Your second charcter class does not include an underscore, but say it does:

m/^[a-zA-Z_]{1}[a-zA-Z0-9_]{7}(\.txt|\.TXT)$/

since you are using string anchors ^ and $ it will match 8 characters and then the file extension. If your regexp is matching filenames with more than 8 characters followed by the extension you are doing something wrong. But since you did not post samples of the data (like I asked) its hard to say. Instead of [a-zA-Z0-9_] you can use \w which is the exact same thing:

m/^[a-zA-Z_]{1}\w{7}(\.txt|\.TXT)$/

when I run that against some test names it works as expected.

Post all of your code and some sample data.

KevinADC 192 Practically a Posting Shark

your question is very hard to not only understand but also to read because you seem to not know how to make sentences that other people can read and make sense of so please edit your post and use proper grammar and post an example of the strings you are trying to match and the ones that think should match and ones you think should not match

KevinADC 192 Practically a Posting Shark

Almost all modules have documentation. And almost all modules are listed on CPAN:

http://search.cpan.org/~timb/DBI-1.607/DBI.pm

There are also many online resources that you can use a search engine to find and many perl books you can purchase or possibly borrow from a library if there is a library/school in your area.

KevinADC 192 Practically a Posting Shark

It looks like it should work but it will not alter the original file if thats what you are thinking. The only reason it will not work is if the regular expression is not finding the search pattern: \b$pattern\b

When posting a question, saying "it does not work" is next to useless. You have to describe what happens when you run the code and post any error messages your code might return.

To search and replace in the same file here is a snippet you can look over:

http://www.tek-tips.com/faqs.cfm?fid=6559

KevinADC 192 Practically a Posting Shark

Next time I would prefer to see some effort to solve your programming reqirements before posting code. Here is one way it can be done:

my $dir = qx{dir};
open (my $OUT, ">", 'dir.txt') or die "$!";
print $OUT $dir;
close $OUT;
KevinADC 192 Practically a Posting Shark

foreach is the wrong command to loop through a file but I am not sure if it is in anyway contributing to the problem. But try this:

while (<INPUTFILE>) {

See if it helps.

KevinADC 192 Practically a Posting Shark

Thats not really enough information but try making the match non-greedy by adding '?':

(\w:\\.*?\.\w{3})

KevinADC 192 Practically a Posting Shark

There is no way to know why the download was slow, that has nothing to do with PPM. Either the downlaod site was busy or your DSL providor was busy or your computer might have been overloaded doing other things.

PPM does have its negative side but also a positive side. The PPM GUI makes things pretty easy in my opinion. The need to add repositories is a bit of a pain, it would be nice if all the modules that have been compiled to use with activeperl were stored in a central repository similar to CPAN. There is also less modules available if you use activeperl but that has nothing to do with PPM itself.

But running the PPM GUI is so simple. Just click on it. It opens, downloads the packlists, synchronizes the data, and then you can see all the modules that are already installed, the ones you can install, you can also uninstall, and more, all in an easy to use GUI interface. Like any software it takes a few tries to learn how to use it, or you could read the instructions.

There is strawberryperl which is a Windows port of perl that uses CPAN just like you would with Unix or Linux. Just don't try and install more than one version of perl on your windows box. It is near impossible to get to work (and might be impossible) given the nature of how Windows works. If you wanted to try …