to parse a string



  • 1. feedback from system() etc. calls?
    I use perl quite often as a glue to tie together command line tools under Linux. Recently I have been using tools that have substantial run times. IN light of this, some of the tools output progress reports, either as moving bars, or just messages at key stages. At the moment, sadly, I only get these messages when the command has finished, which is not so pleasant; I would like to see them as they are generated. Has anyone got a variant of system, popen etc that has worked through the various issues of buffering, differentiating STDOUT, SDTERR etc? Security is not an issue for me; these scripts are running stand alone from the command line. BugBear
  • 2. array of arrays to hash of hashes
    Hi all, I have a category / subcategory file that looks like this cereal cereal:cold:cheerios cereal:cold:corn flakes cereal:hot:oatmeal cereal:hot:cream of wheat bread bread:dakine sweet bread bread:wheat bread:white ...... Anyway you get the picture I read the file in and place into array of arrays open FILE, "$file"; my @file = (<FILE>); close FILE; my @a; foreach (@file){ my @line = split ':', $_; push (@a, \@line); } I want to put this into a hash of hashes where the last element would be equal to 1. something like this $cereal->{cold}->{cheerios} =1; or $bread->{wheat} = 1; if later on down the line the file changes so that there is more subcategories, the 1 would be replaced by a new hash like this $cereal->{cold}->{cheerios}->{regular} =1; $cereal->{cold->{cheerios}->{honeynut} = 1; I'm having trouble writing a function to do this. Any help or ideas would be appreciated
  • 3. "Selling" Perl (i.e. getting the boss to let me install it)
    Hello, I'd like to install ActivePerl on a Windows XP machine specifically to run a particular script. The "problem" is that the admins in charge of the PC are very cautious about what is installed and the security implications of everything (as they should be). I thought I recalled seeing a perlfaq specifically regarding this issue, but several `perldoc -q' searches and a perusal of the perlfaqs posted here were fruitless. (Besides the sort of general "How do I convince others to use Perl?") Can anyone provide a link or perhaps a little narrative about why Perl is safe and secure to install (I'm talking about the Perl interpreter, specifically the ActivePerl build here, not any scripts that may be run). Also, the script that I'd like to run if I do get Perl installed uses WWW::Mechanize. Are there any links, resources, opinions, or first-hand experiences as to the security implications of this particular module? Thanks, PB -- Electricians made popcorn in the power supply
  • 4. EPIC and "my" variables
    I am running eclipse 3.3.2 on MS Vista with cygwin. I have installed padWalker but still can't see the "my" variables when I debug. Any ideas? -- Steve

to parse a string

Postby cc » Sat, 29 Oct 2005 08:36:36 GMT


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"?



Re: to parse a string

Postby A. Sinan Unur » Sat, 29 Oct 2005 09:01:29 GMT

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 

Read about character classes and regular expressions:

perldoc perlre

perldoc perlreref

perldoc perlop

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

Re: to parse a string

Postby cc » Sat, 29 Oct 2005 09:35:40 GMT

> > 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?

Re: to parse a string

Postby A. Sinan Unur » Sat, 29 Oct 2005 09:42:12 GMT

"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 

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

Re: to parse a string

Postby » Sat, 29 Oct 2005 10:50:18 GMT

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
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";


bad numbers:  01010123
replaced:     010101--

Re: to parse a string

Postby Tad McClellan » Sat, 29 Oct 2005 11:59:01 GMT

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

Re: to parse a string

Postby robic0 » Sat, 29 Oct 2005 15:08:39 GMT

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

Re: to parse a string

Postby Tassilo v. Parseval » Sat, 29 Oct 2005 15:59:22 GMT

Also sprach Tad McClellan:

Why the anchoring?

Or use tr:

    print "'$str' is bad data\n" if $str =~ tr/^01//c;

use bigint;

Re: to parse a string

Postby John W. Krahn » Sat, 29 Oct 2005 20:15:04 GMT

Are you sure that caret is supposed to be there?

use Perl;

Re: to parse a string

Postby anno4000 » Sat, 29 Oct 2005 20:21:25 GMT

John W. Krahn < XXXX@XXXXX.COM > wrote in comp.lang.perl.misc:

Yes, in Middle-High-Perl where double negation is the norm.

If you want to post a followup via, 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.

Similar Threads:

1.Parsing a string into an array


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\ line 56.

The line in where it happens is the line of code with the split.


# 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";
             ##$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


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\
         #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

can someone help me or give me a link explaining reg expressions

thanks in advance

4.Module to parse CSV string


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:


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 parsing a string


i have a string


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?

6. Parsing a string [regexp]

7. parsing long strings

8. Parse numeric string

Return to PERL


Who is online

Users browsing this forum: No registered users and 42 guest