Hi, I am new in Perl. Here is my question for help. Given a string, e.g. "01010123", any sample code to scan it and then find the "2" and "3" are not qualified in the string which requires only "0" and "1"? Thanks, mzc.
Hi, I am new in Perl. Here is my question for help. Given a string, e.g. "01010123", any sample code to scan it and then find the "2" and "3" are not qualified in the string which requires only "0" and "1"? Thanks, mzc.
@g44g2000cwa.googlegroups.com: Then this is a good time to read the posting guidelines for this group to learn how you can help yourself, and help others help you. Please note that "write my code for me" requests are not very popular here. Read about character classes and regular expressions: perldoc perlre perldoc perlreref perldoc perlop Sinan -- A. Sinan Unur < XXXX@XXXXX.COM > (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://www.**--****.com/ ~tadmc/clpmisc/clpmisc_guidelines.html
> > I am new in Perl. Here is my question for help. Clearly, this is not what I wanted either; and this is why my sample question from. I tried them before this email. Thank you anyway; can anybody else let me know where I can find those kind of good general sample codes? Thanks.
"cc" < XXXX@XXXXX.COM > wrote in I am not sure what you mean by the above sentence, but you did not include any code. You'd be better off trying to do it, and asking for help with specific issues. Sinan -- A. Sinan Unur < XXXX@XXXXX.COM > (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://www.**--****.com/ ~tadmc/clpmisc/clpmisc_guidelines.html
If you have perl on your system, try to print out the perlcheat.html. On there there are Regex metachars. the [] denote a character class, so if [1a6], if any of these individual characters are contained in what yer checking it is a match. also, '-' is a metachar inside a [] meaning a span of chars so [a-z] is all the chars from a through z. You want to match 2 and/or 3 there, but not 0 or 1. So you want to match [2-9]. Put a couple of forward slashes around it and you have a regex. You might want to read up on it. use strict; use warnings; my $numstr = '01010123'; if ($numstr =~ /[2-9]/) { print "bad numbers: $numstr\n"; $numstr =~ s/[2-9]/-/g; print "replaced: $numstr\n"; } __DATA__ bad numbers: 01010123 replaced: 010101--
The usual idiom for validating data is: anchor the beginning. anchor the ending. write a regex in between that accounts for everything that you want to allow. print "'$str' is bad data\n" unless $str =~ /^[01]+$/; -- Tad McClellan SGML consulting XXXX@XXXXX.COM Perl programming Fort Worth, Texas
On Thu, 27 Oct 2005 21:59:01 -0500, Tad McClellan Well ya know Tad, i wanted to slightly introduce him to metacharacters [] since he might get scared of all the professionalism around hear
Also sprach Tad McClellan: Why the anchoring? Or use tr: print "'$str' is bad data\n" if $str =~ tr/^01//c; Tassilo -- use bigint; $n=71423350343770280161397026330337371139054411854220053437565440; $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
^ Are you sure that caret is supposed to be there? John -- use Perl; program fulfillment
John W. Krahn < XXXX@XXXXX.COM > wrote in comp.lang.perl.misc: Yes, in Middle-High-Perl where double negation is the norm. Anno -- If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers.
1.Parsing a string into an array
Hello I am getting a strange error where I am trying to split a string into an array. The string is a line obtained from doing a directory of a disk. The error message is: Reference to nonexistent group in regex; marked by <-- HERE in m/ Directory of C :\Documents and Settings\Andrew\Application Data\ActiveState\KomodoIDE\4 <-- HER E .1/ at D:\Perl Scripts\remove_duplicate_files.pl line 56. The line in where it happens is the line of code with the split. Thanks, Andrew # # Find all the files # @file_list = `dir c: /S`; # # Build the list of directories and files # $temp_index = 0; $file_index = 0; $directory_index = 0; $dir_list_size = scalar(@file_list); print $dir_list_size; print "\n:"; while ($temp_index < $dir_list_size) { chomp @file_list[$temp_index]; print @file_list[$temp_index]; print "\n"; if (@file_list[$temp_index] ne "") { # # @parse_line = split(@file_list[$temp_index]); # $line_size = scalar(@parse_line); $line_size = 0; if ($line_size > 0) { #print @parse_line[0]; #print "\n"; # # Determine if this is a directory and if so add it to the # directory array and increment the index # if (@parse_line[1] eq "Directory") { #$directory_list = @parse_line[1]; #$directory_index =$directory_index + 1; #print "is a Directory\n"; } else { ##$actual_files[file_index] = @parse_line[0]; # $file_size[file_index] = @parse_line[4]; #$file_deleted[file_index] = 0; #$file_index = $file_index + 1; #print "not a directory \n"; } } } $temp_index = $temp_index + 1; }
2.module for parsing a string into a formula
Sorry if I'm not saying this right... I am coming from a C++ environment, that newsgroup suggested PERL as a possible answer for my need. I have no experience with PERL at all, so will have to do my homework if someone says there is a way. I need a function ( module? ) that can take a string expression "(1 + 2) * (3 + 4)" and return the answer of "21". It can be either a numerical value, or a string value does not matter from this point. Any pointers in the right direction would be welcome. A commercial software package would be acceptable as well. Thank you in advance Michael
3.Parse a String that probably really simple to do
my @content; # Used for parsing HTML # Parse classes @content = split /^/m, $mech->content; for my $i (@content) { # Print class information if ($i =~ /<td align='left' class='PSLEVEL2GRIDROW' height='20'><span class\ ='PSHYPERLINKDISABLED'>/ ) { #parse the string to get only the text in between <td><span> and </br> #then print it #how do i parse the text in between these and print it print "$i\n"; } } the lines im looking for in the html are always...... <td align='left' class='PSLEVEL2GRIDROW' height='20'><span class='PSHYPERLINKDISABLED'>"THE TEXT I WANT FROM THE VARIABLE"</BR> can someone help me or give me a link explaining reg expressions thanks in advance
Hi, I'm looking for a module or a perl code snippet to parse optionally- quoted, comma-separated values. The most obvious candidate, Text::CSV, won't do the job for two reasons: -- it only recognizes double-quotes as the quote character. My input uses single-quotes. -- including the quote character within a quoted value requires doubling the literal quote character (i.e. "I said ""Hi"""). My input uses an escape character ('Henry\'s'). I'd like to be able to parse the following example ... 'George',123,'Harry\'s',,'Tuesday, Thursday' ... into five fields: George 123 Harry's Tuesday, Thursday This seems to me to be relatively straightforward Unix-style (unlike what Text::CSV recognizes, which looks to me more like IBM mainframe syntax). Have I overlooked a tool that will do this for me? Thanks folks.. Chap
hi, i have a string $str="hi|i|am|a|perl|script"; i want to extract the words that are separeated by a '|' into an array. i tried using the split() function, but it worked for cases where the '|' was replaced bya another char like say a ':' can anyone help me in this regard?
Users browsing this forum: No registered users and 42 guest