Pattern Matching 
Author Message
 Pattern Matching

I have a text file in this format:
--(temp.dat)---------------------------



---------------------------------------

I want to fill the hash: my %em_add=();

So far I have this:
--(addess.pl)--------------------------
#!/usr/bin/perl

my %em_add=();
open(FH,/var/temp.dat);
while(<FH>) {
   //Begin Sudo Code using $_
     $em_add{Pesons Name}=Persons Email Address;
   //End Sudo Code

Quote:
}

close(FH);

Please help.  I'm sure its a one line solution.



Mon, 17 May 2004 15:28:35 GMT  
 Pattern Matching


Quote:
> I have a text file in this format:
> --(temp.dat)---------------------------



> ---------------------------------------

> I want to fill the hash: my %em_add=();

> So far I have this:
> --(addess.pl)--------------------------
> #!/usr/bin/perl

> my %em_add=();
> open(FH,/var/temp.dat);
> while(<FH>) {

     $em_add{$1} = $2 if m/^"([^"]*)"\s+(.*)$/;

Quote:
}
> close(FH);

> Please help.  I'm sure its a one line solution.

Yep.

Cheers,
Bernard



Mon, 17 May 2004 15:33:45 GMT  
 Pattern Matching

Quote:
> I have a text file in this format:
> --(temp.dat)---------------------------



> ---------------------------------------

> I want to fill the hash: my %em_add=();

> So far I have this:
> --(addess.pl)--------------------------
> #!/usr/bin/perl

> my %em_add=();
> open(FH,/var/temp.dat);

Check open() for success:

    open(FH,/var/temp.dat) or die "Can't read /var/temp.dat: $!";

Quote:
> while(<FH>) {
>    //Begin Sudo Code using $_
>      $em_add{Pesons Name}=Persons Email Address;
>    //End Sudo Code
> }
> close(FH);

> Please help.  I'm sure its a one line solution.

You can extract the name and the address from $_ using a regex:

    my( $name, $address) = /"([^"]+)" (.*)/;

I'm sure you can take it from there.

The regex could afford some kneading in case the format is not
as strict as assumed.

Anno



Mon, 17 May 2004 15:39:51 GMT  
 Pattern Matching
I have to match a pattern between 1 and 4 digits (Usually 2 or 3) ; if I get
anything else I want to exit. As of know I was putting it into an if
statement as follows:

if ($job  =  /\d\d/ ) {
<Rest of my script>

How can I check to see if I get between 1 and 4 digits effectively?
Any clues would be appreciated.

Joe



Sun, 23 May 2004 19:40:20 GMT  
 Pattern Matching
*** post for FREE via your newsreader at post.newsfeeds.com ***


Quote:
> I have to match a pattern between 1 and 4 digits (Usually 2 or 3) ; if I
get
> anything else I want to exit.
...
> How can I check to see if I get between 1 and 4 digits effectively?
> Any clues would be appreciated.

I recommend:
  perldoc perlretut    (Basic regular expression tutorial).
This includes:
<snip>
        $year =~ /\d{2,4}/;  # make sure year is at least 2 but not more
                             # than 4 digits
        $year =~ /\d{4}|\d{2}/;    # better match; throw out 3 digit dates
        $year =~ /\d{2}(\d{2})?/;  # same thing written differently.
However,
                                   # this produces $1 and the other does
not.
</snip>

That said:

if the string you're matching is in $job:

# determine $job
if ($job =~ /\d{1-4}/) {
   # do your thing

Quote:
} else {

  # don't do your thing

Quote:
}

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 90,000 Newsgroups - 16 Different Servers! =-----


Sun, 23 May 2004 19:56:00 GMT  
 Pattern Matching
perldoc perlre

if ($string =~ /^\d{1,4}$/) {
                ...

Quote:
}

Hope this helps u

Lao



Sun, 23 May 2004 20:14:11 GMT  
 Pattern Matching

Quote:



>> How can I check to see if I get between 1 and 4 digits effectively?
>I recommend:
>  perldoc perlretut    (Basic regular expression tutorial).
>This includes:
><snip>
>        $year =~ /\d{2,4}/;  # make sure year is at least 2 but not more

                       ^
                       ^ comma goes there

Quote:
>if the string you're matching is in $job:

># determine $job
>if ($job =~ /\d{1-4}/) {

                  ^
                  ^   s/-/,/;

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sun, 23 May 2004 22:15:12 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Regular Expressions/Pattern Matching/Unordered pattern

2. pattern as a sentence in pattern matching

3. help with pattern match (repeated patterns)

4. Pattern Match - substitute a string after the match

5. combine two pattern matches to one match?

6. Pattern matching, grabing everything right of the match

7. Pattern matching: are regexes always fastest?

8. pattern matching with non-existence of a string

9. Pattern matching, global and returning list

10. pattern matching question

11. Pattern matching optimizing question (long, with code)

12. Pattern Matching

 

 
Powered by phpBB® Forum Software