415 Posted Topics
Re: [QUOTE=MCLASS80;1769202]I have 2 perl files. 1st file is my main program and the 2nd is a small sub which I call by using system("file.pl"); I would like to reuse a stored value/s in variables from the main program and use them in the external file. How is this possible?[/QUOTE] Main … | |
Re: What do you expect file.pl to do when called that it doesn't seem to do? Do you want control to return to your calling script after the file.pl finishes or just to terminate, or to call another script? Since what someone told you about system didn't work, have you had … | |
Re: I see mitchems has answered your question for installing in Windows. As for ubuntu, I think I installed the DBD::mysql module the lazy way in ubuntu by using the Synaptic Package manager and doing a quick search for libdbd-mysql-perl. You may already have the DBI module installed as it may … | |
Re: I can't reproduce your error. The following script gets a title from a web page and prints it. The output looks OK on my computer.[CODE]#!/usr/bin/perl use strict; use warnings; # define the subclass package IdentityParse; use base "HTML::Parser"; use LWP::Simple; use Encode qw(encode decode); my $printit = 0; sub start … | |
Re: [CODE]#!/usr/bin/perl use strict; use warnings; my %aas; #Hash to store amino acids read_amino_acids('data2.txt'); read_positions('data1.txt'); sub read_amino_acids{ my ($filename) = @_; open my $fh, '<', $filename or die "Failed to open $filename: $!"; my ($name, $key); while (<$fh>){ s/\s+$//; #Remove end-of-line characters my @flds = split /\|/; if (@flds > 1){ … | |
Re: Have you tried [iCODE]$hash->{$col} = eval(qq(\$$col));[/iCODE]? | |
Re: The value of whatever matches what is in the first pair of parentheses goes into $1, and the value of whatever matches what is in the second pair of parentheses goes into $2. Here's an example of capturing matches into the $1 and $2 variables.[CODE]#!/usr/bin/perl; use strict; use warnings; my … | |
Re: [QUOTE=fredfletcher;1758033]Any chance to get a rewrite of a script?[/QUOTE] You already know how to do the CGI which I think must be logically independent of how to prepend a string to a file.[CODE]#!/usr/bin/perl; use strict; use warnings; my $filename = 'data.txt'; #If file doesn't exist, create it if (-e $filename){ … | |
Re: An associative array, otherwise known in Perl as a hash, does not preserve the order in which the elements were added. If you want to list the contents of a hash in a particular order you can sort the keys according to rules which give you the desired result. If … | |
Re: [CODE]#!/usr/bin/perl; use strict; use warnings; my $filename3 = 'file_out.txt'; my $filename = 'dna.txt'; open my $fh, '<', $filename or die "Failed to open $filename: $!"; open my $fho, '>', $filename3 or die "Failed to open $filename3: $!";#Open file for output my $name; while (my $rec = <$fh>){ $rec=~ s/\s//g; chomp($rec); … | |
Re: Your script looks up each name in a hash before inserting it into your database and saving it into the hash. That works as long as your script runs, but when it stops your hash no longer exists. The database persists when the script isn't running, so you need to … | |
Re: [QUOTE=biojet;1749758]...I can put out the data with [CODE] num name product star_posi end_posi 1 [gene=KIQ_00005] [protein=hypothetical [location=complement(<1..423)][/CODE] but I can not seperare the number of [location] and I can not delete [] of data...[/QUOTE]To remove the square brackets from your text string you can do a regex substitution.[CODE]#!/usr/bin/perl; use strict; … | |
Re: [CODE]#!/usr/bin/perl; use strict; use warnings; my $filename1 = 'file1.txt'; my $filename2 = 'file2.txt'; my $filename3 = 'file3.txt'; #Build a hash of arrays from file2 my %uniprot_families; open my $fh, '<', $filename2 or die "Failed to open $filename2: $!"; while (my $rec = <$fh>){ chomp($rec); my ($id, $family) = split(/\s+/, $rec); … | |
Re: [CODE]#!/usr/bin/perl; use strict; use warnings; use Data::Dumper; my $n = 3;#Decided position my $filename = 'sample file.txt'; open my $fh, '<', $filename or die "Failed to open $filename: $!"; while (<$fh>){ next if m/^#/;#Skip comment lines my @array = (); foreach (1 .. $n){ my $rec = <$fh>; last unless … | |
Re: [CODE]#!/usr/bin/perl; use strict; use warnings; printf("Dollar %s\nPound %s\nPercent %s\nCarot %s\nAmpersand %s", ('$', '#', '%', '^', '&', '*')); #Outputs #Dollar $ #Pound # #Percent % #Carot ^ #Ampersand &[/CODE]I'm not sure I understand the question. You ask about symbols occurring within the text but your example shows a percent sign occurring … | |
Re: You need to save your base information into a variable as you read it, and save this into one of your hashes, along with position.[CODE]#!/usr/bin/perl; use strict; use warnings; use autodie; my ( %data1, %data2 ); open my $in, '<', 'baselist.txt'; while (<$in>) { next unless /^\s*\d/; my ( $num, … | |
Re: I don't see how your script decides whether to print 'change' or 'same'? What is the rule that determines this? | |
Re: [QUOTE]Every Perl expression is in one of two `contexts', either `list context' or `scalar context', depending on whether it is expected to produce a list or a scalar. Many expressions have quite different behaviors in list context than they do in scalar context.[/QUOTE] [URL="http://perl.plover.com/context.html"]http://perl.plover.com/context.html[/URL] The print command expects a list … | |
Re: input1.csv[CODE=text]3 ATG 2 ACT 1 ATC [/CODE] input2.csv[CODE=text]G C C A A A [/CODE] [CODE]#!/usr/bin/perl; use strict; use warnings; my ($filename1, $filename2) = ('input1.csv', 'input2.csv'); open my $fh1, '<', $filename1 or die "Failed to open $filename1: $!"; open my $fh2, '<', $filename2 or die "Failed to open $filename2: $!"; while … | |
Re: Did you run it with at least two command line arguments? When I run your script from the command line with no arguments I get the same error. But if I run it from the command line as follows:[iCODE]perl temp01.pl localhost 42;[/iCODE] it runs and prints lines until I kill … | |
Re: [QUOTE=sandeepau;1716386]Hello, Can someone tell me the equivalent perl script/command for following unix command: sort -t"|" -k1,1 -T '/temp' input.txt > output.txt Here, I want mention different physical directory for temprary sort file storage. like - T in unix shell command. In other word, How to mention different workspace directory in … | |
Re: Could you give us a snippet of your script to illustrate what you are trying to do? Maybe it's me but I really don't understand what you mean by evaluating "variabl%e". Does the following bear any resemblance to what you want?[CODE]#!/usr/bin/perl; use strict; use warnings; my $variable = 1; my … | |
Re: Why do you say it doesn't compile? When I run it with options -H localhost -t load, for example, it just hangs until I kill it. I don't get any syntax errors indicating it won't compile and no runtime errors such as division by zero, so I can't reproduce the … | |
Re: Run Perl with the -d option and it starts in [URL="http://perldoc.perl.org/perldebug.html"]debug mode[/URL] which you can use as a command line interpreter to print out the values of expressions. The 'p' command tells the Perl debugger to print the value of the expression.[iCODE]david@david-laptop:~$ perl -de 0 Loading DB routines from perl5db.pl … | |
Re: [CODE]#!/usr/bin/perl; use strict; use warnings; my $filename = 'input.csv'; open my $fh, '<', $filename or die "Unable to open $filename: $!"; my $firstline = <$fh>; chomp($firstline); print "The first line is $firstline\n"; my @fields = split(/,/, $firstline); my $len = length($fields[1]); print "The second field contains '$fields[1]' which has length … | |
Re: [CODE]#!/usr/bin/perl; use strict; use warnings; my $pathway;#To save string from previous iteration, create variable outside loop while (my $line = <DATA>) { chomp($line); if ($line =~ /^$/) { next; } elsif ($line =~ /ko/) { $pathway = $line; } elsif ($line =~ /K/) { #trim($line); #What does your trim() subroutine … | |
Re: Building on [URL="http://www.daniweb.com/members/Trentacle/865513"]Trentacle[/URL]'s first correction of your data structure you could try the following:[CODE]#!/usr/bin/perl; use strict; use warnings; use Data::Dumper; my $chainStorage = { ACB => { E => { '06' => [100, 200, 95] }, B => { '23' => [20, 1000, 5, 30] }, }, AFG => { … | |
Re: [CODE]#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $rec = '(9430, 3656) (9147, 14355) (133, 14393) (7917, 9513) (3719, 12775)'; $rec =~ s/['(),]//g; #Remove everything except digits and spaces my %hash = split / /, $rec; #Split on space character and assign to hash print Dumper(\%hash);[/CODE] | |
Re: We can take the greatest five from a hash without sorting. I don't know if that makes it more efficient. For example, the following prints the five colors having the longest wavelengths.[CODE]#!/usr/bin/perl use strict; use warnings; my %d = (violet => 400, red => 650, indigo => 445, orange => … | |
Re: Here's an example of sorting an array by a specified column.[CODE]#!/usr/bin/perl; use strict; use warnings; my @array = sort howtosort <DATA>; foreach (@array){ chomp; print "$_\n"; } sub howtosort{ my @flds_a = split(/\|/, $a); my @flds_b = split(/\|/, $b); $flds_a[2] cmp $flds_b[2]; #compare key fields to sort } __DATA__ 1780438|20110709|0000007704000000000000004888|7704|48881|PE|08/12/2008 … | |
Re: [CODE]#!/usr/bin/perl use strict; use warnings; my ($file1, $file2, $file3) = qw(1.txt 2.txt 3.txt); open my $fh1, '<', $file1 or die "Can't open $file1: $!"; open my $fh2, '<', $file2 or die "Can't open $file2: $!"; open my $fh3, '>', $file3 or die "Can't open $file3: $!"; while (<$fh1>){ last if … | |
Re: [QUOTE=ssdeep;1704055]i want to be able to treat contents of a file as an array and traverse through it that way without having to store them in an array,can i do that?if so how?[/QUOTE] [URL="http://perldoc.perl.org/Tie/File.html"]Tie::File[/URL] treats the contents of a file as an array, but not a 2D array. You might … | |
Re: You need to name your DATA section [ICODE]__DATA__[/ICODE] in capital letters, not [ICODE]__data__[/ICODE]. You can skip records that don't begin with a number, as follows:[CODE]#skip non-numeric data next unless m/^\d/;[/CODE] Notice that the script ignores column 0 and starts with column 1, because in Perl array indexes begin at 0. … | |
Re: Only very old versions of Perl require you to name file handles as barewords such as FILE1 etc. Use lexical filehandles instead. [QUOTE]FILEHANDLEs without sigils are a sign of ancient Perl. [URL="https://www.socialtext.net/perl5/bareword_uppercase_filehandles"]This page[/URL] aims to describe why this style was once common, what's wrong with it, and what you can … | |
Re: What part of the script gives you trouble? Reading a file? Splitting the data and saving them in an array? Writing a new file? | |
Re: I don't have Windows, but I think that instead of using the ODBC driver you may have more success using the DBD::mysql driver. For instructions see [URL="http://cpansearch.perl.org/src/JWIED/DBD-mysql-2.1012/INSTALL.html"]How to install and configure DBD::mysql[/URL] | |
Re: Post your SQL statement here, wrapped in [noparse][CODE]your SQL statement[/CODE][/noparse] tags and maybe someone here will be able see what's wrong with the syntax. | |
Re: For example:[CODE]#!/usr/bin/perl use strict; use warnings; use File::Find; use File::Path qw(make_path); use File::Basename; use File::Copy; my @directories_to_search = ('/home/david/Programming/data'); my @suffixes = qw(.axf .fls .txt .err .dummy .log .cfg); find(\&wanted, @directories_to_search); sub wanted{ my($filename, $directories, $suffix) = fileparse($File::Find::name, @suffixes); if ($suffix){ my $target_dir = "/home/david/test/$directories"; make_path($target_dir); copy($File::Find::name,"$target_dir/$filename$suffix") or die "Copy … | |
Re: Suppose file.txt contains the following:[iCODE]CTTA TAAC GACC CCCG CCGA CACG GCAG TGAG CGCA GCAG CGAC GCGT GGCT CTTG TAAT AACC AATG CGCT TGCG AAAT CAGC TAGC CCAT TTGA TAAA GTAA GGGC TCGA GAGG ATTT GGCA TTAA GCAC GGCT TGTG CCTA CCTC TGGT TTCC GTGT CTAC ACAG TAGT CGGC TGTC TATC … | |
Re: [iCODE]$sql = "INSERT INTO leaderboard ([COLOR="Red"]First Name[/COLOR]) VALUES ('values')";[/iCODE] When a column name contains a space you must quote it using back ticks, like this: [iCODE]$sql = "INSERT INTO leaderboard ([COLOR="Green"]`First Name`[/COLOR]) VALUES ('values')";[/iCODE] | |
Re: [CODE]#!/usr/bin/perl use strict; use warnings; while(my $rec = <DATA>){ chomp($rec); #the // default delimiters for a match can be changed to arbitrary delimiters #The following regex is delimited by {} because pattern contains forward slashes $rec =~ s{/English-Folder/(.+)_e\.shtml}{/French-Folder/$1_f.shtml}; print $rec, "\n"; } __DATA__ /English-Folder/temp.php.u1conflict /English-Folder/temp-post01.html /English-Folder/temp-post.html /English-Folder/temp.txt /English-Folder/target01_e.shtml /English-Folder/testcomponent.html /English-Folder/target02_e.shtml … | |
Re: [QUOTE=Rogue45;1637834]I wrote c program that outputs all permutations of a word to a txt file. ate aet tae tea eat eta I also have a txt file of all the words in the dictionary. I would like to take the first entry in permutations.txt and search dictionary.txt to see if … | |
Re: Please attach your data as *.txt file(s) instead of images or *.pdf files. Instead of having to look at the images and manually enter your data into MySQL we could copy and paste it to try and reproduce the error. | |
Re: Your loop runs while($run eq "yes") but when the user enters yes at the prompt then $run contains yes with a newline character at the end. Try doing a chomp on the $run after assigning the STDIN to it.[CODE]print "Send more strings? yes/no"; $run = <STDIN>; chomp($run);#Remove the newline character … | |
Re: [CODE]#!/usr/bin/perl #print_files_in_subdirs.pl use strict; use warnings; my $startdir = '/home/david/Programming/data'; print_files($startdir); sub print_files { #This subroutine calls itself for each subdirectory it finds. my $dir = $_[0]; my @files = <$dir/*>; my @d; foreach (@files) { next if $_ eq "." or $_ eq ".."; my $fn = $_; if … | |
Re: [QUOTE=bio-grad;1617049]perhaps, rather than a b-tree, someone could help me put together a method of finding the "gene" with the minimum distance to the "hairpin" by doing something like my @dist = ( abs($h_hash{$c[1]}[$i][2] - $c[2]), abs($h_hash{$c[1]}[$i][3] - $c[2]), abs($h_hash{$c[1]}[$i][2] - $c[3]), abs($h_hash{$c[1]}[$i][3] - $c[3]) ); @dist = sort {$a <==> … | |
Re: I don't think you can re-use a result set with DBI base on this quote from [URL="http://docstore.mik.ua/orelly/linux/dbi/ch05_01.htm"]this tutorial[/URL]: [QUOTE]Cursors are used for sequential fetching operations: records are fetched in the order in which they are stored within the result set. Currently, records cannot be skipped over or randomly accessed. Furthermore, … | |
Re: [CODE]#!/usr/bin/perl; use strict; use warnings; my $filename1 = 'Data_1.txt'; my $filename2 = 'Data_2.txt'; my %ranges; open my $fh, '<', $filename2 or die "Failed to open $filename2: $!"; while (<$fh>){ next unless m/^\d/; #skip unless line starts with number chomp; my ($num, $start, $end) = split; $ranges{$num}{Star_posi} = $start; $ranges{$num}{End_posi} = … | |
Re: I don't understand the 31 versus 32 bit distinction well enough to give a complete solution but here is a partial one:[CODE]#!/usr/bin/perl use strict; use warnings; my @examples = ('255.102.25.02/32', '255.102.25.02/31'); my @results; foreach my $example(@examples){ my ($ip, $bits) = split /\//, $example; if ($bits == 32){ push @results, convert32($ip); … |
The End.