415 Posted Topics
Re: For the purpose of giving a simple example of extracting more than one instance from a string, I hard-coded a sample string instead of reading from your files. Notice that you can store the instances in an array instead of a single scalar variable.[CODE]#!/usr/bin/perl use strict; use warnings; my $string_of_instances … | |
Re: Initialising the array in advance with a loop doesn't seem like a bad approach, either. It takes only about one line.[CODE]#!/usr/bin/perl use strict; use warnings; my @data = (); #Empty array with no elements my $how_many_bins = 25; #Arbitrary example #The following loop pushes a zero into the array for … | |
Re: [QUOTE=aquilax;1330883]Check if executing: [CODE]SET names utf8[/CODE] after connecting to the database will fix the problem.[/QUOTE]Yes. Apparently this is an MySQL bug, according to this link: [URL="http://bugs.mysql.com/bug.php?id=10812"]http://bugs.mysql.com/bug.php?id=10812[/URL] | |
Re: Try the following. I made a couple of changes to your script, indicated by comments. I changed the regex slightly because [iCODE]$f=~s/<!--[^>]*-->//g;[/iCODE] will not remove comments if the character '>' occurs anywhere between the comment tags. It's better to use a dot that represents all characters.[CODE]#!/usr/bin/perl use strict; use warnings; … | |
Re: [CODE]<?php require('includes/init_mysql.php'); $con = mysql_connect($db_host,$db_user,$db_pass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("daniweb", $con); $sql = "SELECT SUM(ct) FROM `test2`"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "Total ". $row['SUM(ct)']; } mysql_close($con); ?>[/CODE]This displays [iCODE]Total 6[/iCODE] on my browser. My code is the same as … | |
Re: [CODE]<?php $full = '%B1500000000000015^EMPLOYEEID/SMITH John^031110100000019301000000877000000?;500000000000015=0305101193010877?'; $chunks = explode('?', $full); echo $chunks[0] . '?'; ?>[/CODE]It looks like you want the value of $StringVar2 to equal that of $StringVar1. Is that correct? | |
Re: I haven't tested it but I think that either using DISTINCT or GROUP BY should eliminate the duplicate rows from your results. Try [CODE]SELECT DISTINCT clients.ClientID, clients.ClientUserID, clients.AddedByUserID, users.UserID, users.FullName, users.AccessLevel FROM users LEFT JOIN clients on users.UserID = clients.ClientUserID WHERE users.AccessLevel = 1 or (clients.ClientUserID = users.UserID)and clients.ClientID = … | |
Re: The first part, storing the contents of the files into string variables and then extracting the module code from each into two string variables:[CODE]#!/usr/bin/perl #extract_mods_to_strings.pl use strict; use warnings; my $dir = '/home/david/Programming/Perl/data'; my $filename1 = '1.txt'; my $filename2 = '2.txt'; #The following way of 'slurping' files into strings adapted … | |
Re: [CODE]#!/usr/bin/perl use strict; use warnings; my %years; #Hash to save year as key, count as value while (<DATA>){ chomp; $years{$_}++; } printf "%-10s %s\n",'year', 'count'; foreach (sort keys %years){ printf "%-10s %d\n",$_, $years{$_}; } __DATA__ 1913 1913 1917 1917 1917 1917 1917 1955 1955[/CODE] | |
Re: What database are you using? I ran something similar to the above in MySQL and didn't get the error. When I used to work with Oracle and MS-Access databases I remember getting errors like that. If you are using MySQL as your database, do you by any chance have ONLY_FULL_GROUP_BY … ![]() | |
Re: I am worried that retiring from IT contract work in 2006 without a pension may not have been a great idea, since the supply of qualified IT professionals now seems to greatly exceed the demand around here (Brockville, Ontario, CANADA). However, I like having spare time so if my wife … | |
Re: I think you need some hidden input fields in the form with the submit button that posts back to your script. Otherwise the form is posting nothing except the value of the submit button, right? Hidden input fields allow you to repost your data.[CODE]<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" <input type="hidden" … | |
Re: Did you remove the # from the lines containing the paths and names of your files? Line 13 is trying to open the file name stored in $f1. Mike commented out the paths to your files so the script would run on his computer. | |
Re: First show us code from the page you are running. For example, are you trying to access a page called example.php in your browser? Show us the what is in example.php if it's not too big. Otherwise show us the relevant code that specifies or attempts to navigate to a … | |
Re: Sorry, this question is way over my head. I got as far as plotting your data but it doesn't look like a bell curve... more like twin peaks. In case it helps as an example of a line graph, here's the script:[CODE]#!/usr/bin/perl #bell_curve_line.pl use strict; use warnings; use GD::Graph::lines; my … | |
Re: You only need one query which you run once. Then you fetch the results into an array and loop over the array.[CODE=php]<?php require('includes/init_mysql.php'); $con = mysql_connect($db_host,$db_user,$db_pass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("daniweb", $con); $key = array('Cable Guy', 'John Doe'); foreach($key as $value){ $dr[] = "name … | |
Re: I think the problem is that your CASE statement is evaluating to the number 1 instead of the string that you want for completing your WHERE condition. Look at this example:[CODE]mysql> select '07:20:00' AND '15:19:59' as shift; +-------+ | shift | +-------+ | 1 | +-------+ [/CODE]See? The result is … | |
Re: [QUOTE=jjemphoung;1314437]good day! im an IT student. im new to php&mysql. my database engine is first myisam. everything works fine. but when i changed it into innodb(for foreign key purposes) i can't insert a data in my the parent table. i can insert data in child table though. replies are really … | |
Re: Thanks for the link Mike. One of my favourites is $. although I haven't managed to use it much yet. $. Is not a regex variable but here is an example of using it to identify the line numbers where a regex match succeeds.[CODE]#!/usr/bin/perl use strict; use warnings; while (<DATA>){ … | |
Re: I don't know how to print what you want yet but to figure it out I might start by using Data::Dumper to print the data structure and values.[CODE]#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $referenceTable; my @port = qw(80 70 1083 42); my ( $key1, $key2 ) = ( … | |
Re: [CODE]#!/usr/bin/perl use strict; use warnings; #I'm reading my __DATA__ section but you can open your file # instead and slurp it as follows: (I don't read a list of duplicates, only the first file) undef $/; my $whole_file = <DATA>; # 'slurp' mode $/ = "\n"; #Put it back the … | |
Re: There should be a way to incorporate a [URL="http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#operator_case"]CASE expression[/URL] into your UPDATE query. Also, consider using [URL="http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between"]BETWEEN min AND max[/URL]. | |
Re: ghosh22: Make sure your input file is [I]really[/I] tab-delimited (see attached file) rather than space-delimited. mitchems already pointed this out, but some text editors make it hard to see the difference so you may not have understood what he meant. mitchems: I added a line to your script to satisfy … | |
Re: Yes, you need to have write permission for the file you want to delete, and according to a poster at the following link: [URL="http://www.php.net/manual/en/function.unlink.php#53027"]"The folder that contains the file must ALSO have write permission."[/URL] | |
Re: I don't see the need to record rank in your database table because you can calculate it from the scores, right? Plus I would not put the teams in different columns on one record because that makes it difficult to write queries on the data. I think a table like … | |
Re: [CODE]#!/usr/bin/perl #print_last_2_lines.pl use strict; use warnings; my $previous; #Declare $previous and $latest outside the loop my $latest; # because we want to print them outside the loop while (<DATA>){ #Loop through file, saving last two lines $previous = $latest; $latest = $_; } print $previous, $latest; __DATA__ StartTimeStamp,EndTimeStamp,counter1,counter2 Aug15 2010 … | |
Re: [CODE]<?php function string_of_xes($matches) { // as usual: $matches[0] is the complete match $r = str_repeat('x', strlen($matches[0])); return $r; } $pattern = '/0{5,}/'; $string = '11000001100010011000001'; //Replace all strings of 0's with length 5 or longer with same # of x's $temp1 = preg_replace_callback($pattern, string_of_xes, $string); echo $temp1 . "\n"; //Replace … | |
Re: First, make a small change to your print_result subroutine (really, just uncomment a line that was commented out).[CODE]sub print_result { my ($baseurl, $file, $title) = @_; $file =~ s#^/##; $baseurl =~ s#/$##; $The_Count++; #Add one to your count of recipes listed print qq(<li><a href="$E{"$baseurl/$file"}">$E{$title}</a></li>\n); }[/CODE]Second thing to do is scroll … | |
Re: I don't have an answer of my own but someone asked the same question on [URL="http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php"]http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php[/URL] and the answers seemed to involve doing pretty much what you're doing, i.e. writing a function to do it. You may want to have a look at the proposed solutions there and compare them … | |
Re: [CODE]#!/usr/bin/perl #getopt03.pl use strict; use warnings; use Getopt::Long; our ($opt1, $opt2, $opt3); our @opt = ( \$opt1, \$opt2, \$opt3 ); GetOptions( 'opt1=i' => \$opt1, 'opt2=i' => \$opt2, 'opt3=i' => \$opt3 ); check(); ## I don't see a problem. The actual options do reflect the changes. print "The actual values of … | |
Re: I'm not comfortable with setting up key index columns but I may be able to answer some questions about a hash. With what about the concept of hash are you not comfortable? The main things you need to know, in order to read a file into a hash are[LIST] [*]How … | |
Re: [CODE]<?php $ct = array ( "a" => 1, "b" => 2, "z" => 2, "c" => 1 ); $con = mysql_connect('localhost','david','mypwd'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("daniweb", $con); foreach($ct as $item => $count){ $sql = "INSERT INTO test2(item,ct) VALUES ('$item', $count)"; if ($result = mysql_query($sql)) … | |
Re: If you want to use PHP to create a string containing a SELECT query containing both values in a $day_result array, you could do it like this:[CODE=php]<?php $day_result = array(1, 4); // Initial array of numbers $sql3 = "Select * from Venue where "; foreach($day_result as $day_num){ $dr[] = "idVenue … | |
Re: [iCODE]opendir(FOLD, [COLOR="Red"]@fld[/COLOR]);[/iCODE] First error I see is that opendir expects an expression naming [I]one[/I] directory. Any variable starting with @ contains an array whereas opendir needs a string representing the name of [I]one[/I] directory only. | |
Re: Regarding doubt #3, I think you would have to write conditional logic to allow the -r (rename) parameter only if the -d (directory) parameter is specified. Maybe roughly like the following:[CODE]#!/usr/bin/perl #getopt01.pl use strict; use warnings; use Getopt::Long; my ($help, @dir, @rename_to); #-- prints usage if there is an unknown … | |
Re: If the OP had enabled warnings, and named the script something other than a number, then the following warning would have been helpful: [COLOR="Red"]Argument "/home/david/Programming/Perl/weird.pl" isn't numeric in array element at /home/david/Programming/Perl/weird.pl line [whatever].[/COLOR] | |
Re: You could alter your SELECT statement to explicitly name the columns you want in your result set (instead of *) and include a calculated column which you would specify something like this:[CODE=mysql]SELECT order.item_name, order.quantity, item.price, order.quantity*item.price AS value FROM fb.order LEFT JOIN item on order.item_name = item.item_name WHERE fb.order.order_status = … | |
Re: [QUOTE=Spakes;1280310]Hi , How do I write a code that will print the first 10 even numbers in PERL Thank you[/QUOTE]Here is one way:[CODE]#!/usr/bin/perl use strict; use warnings; map {print $_ * 2 . "\n"} (1..10);[/CODE] | |
Re: Have you looked at the one on [URL="http://dennismueller.org/blog/2009/03/csv-to-vcard-conversion-tool/"]http://dennismueller.org/blog/2009/03/csv-to-vcard-conversion-tool/[/URL]? You don't have to use it online. He also gives a [URL="http://dennismueller.org/blog/tools/vcard/csv-to-vcard/"]link[/URL] from which you can download the source and run it locally. I tried downloading and extracting it. On my Linux platform I had to change the permissions for the folder … | |
Re: Maybe something like this?[CODE]<?php $url = "http://localhost/temp-hum-light.html"; $str = file_get_contents($url); if (preg_match('/(?:uN703610TF:\s*(\d+\.\d*))/', $str, $matches)) { $temp = $matches[1]; } if (preg_match('/HU:\s*(\d+\.\d*)/', $str, $matches)) { $hum = $matches[1]; } if (preg_match('/IL:\s*(\d+\.\d*)/', $str, $matches)) { $light = $matches[1]; } echo '$temp is ' . "$temp\n"; echo '$hum is ' . "$hum\n"; echo … | |
Re: Using the sample data you provided that has two columns delimited by a space, running the following script:[CODE]#!/usr/bin/perl use strict; use warnings; *ARGV = *DATA if scalar(@ARGV) == 0; my %count; #Hash containing count of occurences of each unique value while (<>) { my ($col_x_val, $col_y_val) = split; #splits record … | |
Re: [CODE]mysql> GRANT SELECT ON daniweb.people TO jess@localhost IDENTIFIED BY "jessrules"; Query OK, 0 rows affected (0.00 sec) mysql> select User from mysql.user; +------------------+ | User | +------------------+ | root | | root | | david | | debian-sys-maint | | jess | | root | +------------------+ 6 rows in set … | |
Re: Are you sure $contents contains the string you say it does? When I manually assign the string you want to match to $contents the match works and there is something in the $out array.[CODE]<?php $contents = <<<END <a href="./viewtopic.php?f=15&t=119871" class="topictitle">Twilight Graphic Novel</a> END; preg_match_all("/<a href=\".\/(viewtopic.php\?f=15&t=[0-9]+)\" class=\"topictitle\">([#\w\s_\-!$%&()?.,'\":;{}\/\\|’=\[\]+\–\™ ]+)<\/a>/i",$contents,$out); $len=count($out[0]); echo '$len … | |
Re: [QUOTE=mroberts;1272577]I need some help. I have figured out most of my script but I need to search a string and see if it starts with certain characters and Does not end in another. [$line1 = "EB*1**96" #should produce true value and blank line] [if ($line1 =~ /^EB\*/ and $line1 ne … | |
Re: Are you quite sure the hospital will never have to admit more than 999 new patients during a single day, at least not until you have retired?:) You could create another table to store the last patient id used. Add only one row to this table. Whenever a row is … | |
Re: The following reads each line from an input file, removes the specified pattern from each line, if found, and prints each line to standard output, which could easily be redirected to a file.[CODE]#!/usr/bin/perl use strict; use warnings; *ARGV = *DATA if scalar(@ARGV) == 0; while (<>) { s/<HTML>//g; print;} __DATA__ … | |
Re: Is it too late to change the design of the 'children' table? Your problem comes from there. Why not have only two columns? One column would identify the user and the other the name of one child. Each user could have as many rows in the children table as needed … | |
Re: Apparently, part of the problem starts at this statement: [iCODE]@arr=split("",$_); #This splits the string into single characters[/iCODE] To test, I ran the following: [CODE]print join("\n",split("",'abcdefghijklmno'));[/CODE] And the output was[CODE]a b c d e f g h i j k l m n o [/CODE]I know that doesn't help much. If … | |
Re: [CODE]#!/usr/bin/perl use strict; use warnings; #For testing, let's assign a string constructed with q(), which puts single #quotes around text that may contain quotes. You can read from your file instead. my $rec = q("CN=BERRY Richard,OU=TestFinance,OU=HR,OU=CORP,OU=CR,DC=mycorp,DC=com","BERRY Richard",BERRY,FR,"Puylouvier",FRANCE,,56000,"+33 1 23 45 67 89","+ 33 1 23 45 67 80",Richard,FRANCE,IT,X'332c206176656e7565204e6577746f6e0d0a',Bob.Malone,Richard.BERRY@mycorp.net," -",); #Remove … | |
Re: I pasted some text into the __DATA__ section at the end of my script for convenience but the following will work just as well if you read from a real text file.[CODE]#!/usr/bin/perl #TestForAsterisks.pl use strict; use warnings; sub is_separator { my $line = shift; #Assign passed argument to variable if … |
The End.