Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Last Letters

by ImpalaSS (Monk)
on Nov 30, 2000 at 00:20 UTC ( [id://43993]=perlquestion: print w/replies, xml ) Need Help??

ImpalaSS has asked for the wisdom of the Perl Monks concerning the following question:

Hey,
I am sure this is really simple, but i cant figure out a way to do this fast.
What i have are names, such as:
pa0002Yeadon.2(5091) pa0003Presidential.1(2664) pa0014Haverford.1(2589)
Only, i have about 1200 of them, all variable lenghts. I need a way to extract the number within the ()'s from everyone? I tired the substr() method, but that starts from the beginning, or i need to know the lenght of the total string. Any ideas?

PS (if there is a way to also get out the 4 digit number (0002 for yeadon) and add it to the adove simply, that would also help
THanks a million

Dipul

Replies are listed 'Best First'.
Re: Last Letters
by kilinrax (Deacon) on Nov 30, 2000 at 00:22 UTC
    Try using a regular expression:
    #!/usr/bin/perl -w use strict; my @data = qw|pa0002Yeadon.2(5091) pa0003Presidential.1(2664) pa0014Haverford.1(2589)|; my @numbers = map { /\((\d+)\)/ } @data; print "@numbers\n";

      Dang, beat me to it =) I note, for the record, that killinrax's solution which is elegant, assumes you know that the first thing in your lines of data that are going to be wrapped in parentheses are the numbers you seek, AND that every line will contain a number (which may or may not be a problem; but if you want to do 1:1 matching of numbers onto lines, you'd like to know which line is missing a number if the sizes of @numbers and @data don't match up.)

      In other words: it's a great basic technique, but be careful how you implement it!

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Last Letters
by lemming (Priest) on Nov 30, 2000 at 00:31 UTC
    My day for playing with substr
    substr($string, -5, 4);
    Negative numbers count from the end of the string for position. Note that this only works if you have a 4 digit number with another character on the end.
Re: Last Letters
by Fastolfe (Vicar) on Nov 30, 2000 at 00:24 UTC
    Why can't you do this:
    $idx = index($string, '(') + 1; $num = substr($string, $idx, length($string) - $idx - 1);
    Or:
    ($num) = $string =~ /\((\d+)\)/; # Plus the year ($year, $num) = $string =~ /(\d+)[^(]*\((\d+)\)/;
Re: Last Letters
by swiftone (Curate) on Nov 30, 2000 at 00:25 UTC
    Assuming there are no other parens, you can do:

    ($number)=$string=~/\(([^)]*)\)/;

    If that assumption is false, reverse it:

    $string = reverse $string; ($number)=$string=~/\(([^)]*)\)/; $number = reverse $number;
      Even if there are other parens, reversing the string twice is a very weird way to get the data. Just use $ to anchor the match to the end of the string:
      ($number) = ($string =~ /\( (\d+) \) # number in parentheses [^()]* $/x); # at the end of the string
        Oops, you're right. I tend to overuse reversing ever since I found it to be such a nice solution to the comma-fying a number FAQ.
Re: Last Letters
by ImpalaSS (Monk) on Nov 30, 2000 at 00:27 UTC
    Ooops, i forgot. Each of the names above, are strings, they arent all part of an array. Also, scratch when i said extract the 4 numbers from the name, cause thats easy using substr() :).
    Thanks Again

    Dipul

      The key is the regular expression, which grabs the numbers out of the line (assuming your data is sane)

      while (<FILE>) { # $number will only get set if the current line contains one or mo +re digits in between parentheses my ($number) = /\((\d+))/; # do something with $number }

      I'd suggest reading perlre (but check your own system's docs) and poke around for phrases like "regular expression memory", which is the technique being employed here.

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://43993]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found