Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Regexp help

by hey_frind (Initiate)
on Nov 22, 2012 at 00:36 UTC ( #1005032=perlquestion: print w/ replies, xml ) Need Help??
hey_frind has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I started using Regexp recently.

Here is my Input:

0.740234|0.758789|0.748047|0.749023|0.748047|0.754883|0.743164|0.748047|0.780273|0.739258|0.765625|0.761719|0.760742|||||0.75293|0.742188

what is the correct regexp to use to get these columns delimited into 19 columns out of which columns 14,15,16,17 are null?

Thanks,

Comment on Regexp help
Re: Regexp help
by frozenwithjoy (Curate) on Nov 22, 2012 at 01:02 UTC
    Why not use split?
    $ perl -E '$string = "0.740234|0.758789|0.748047|0.749023|0.748047|0.7 +54883|0.743164|0.748047|0.780273|0.739258|0.765625|0.761719|0.760742| +||||0.75293|0.742188"; say $_ for (split /\|/, $string); ' 0.740234 0.758789 0.748047 0.749023 0.748047 0.754883 0.743164 0.748047 0.780273 0.739258 0.765625 0.761719 0.760742 0.75293 0.742188

    NOTE: Keep in mind that when matching | in split (or another regex situation), you need to escape it: \|

Re: Regexp help
by 2teez (Priest) on Nov 22, 2012 at 01:09 UTC

    split perl function will conveniently do like so:

    use warnings; use strict; use Data::Printer; my $str='0.740234|0.758789|0.748047|0.749023|0.748047|0.754883|0.74316 +4|0.748047|0.780273|0.739258|0.765625|0.761719|0.760742|||||0.75293|0 +.742188'; my @str_data = split/\|/,$str; p @str_data;
    Output:
    [ [0] 0.740234, [1] 0.758789, [2] 0.748047, [3] 0.749023, [4] 0.748047, [5] 0.754883, [6] 0.743164, [7] 0.748047, [8] 0.780273, [9] 0.739258, [10] 0.765625, [11] 0.761719, [12] 0.760742, [13] "", [14] "", [15] "", [16] "", [17] 0.75293, [18] 0.742188 ]

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Regexp help
by muba (Priest) on Nov 22, 2012 at 01:23 UTC

    First of all, what do you mean by null? I will assume you mean undef, but you might as well mean the numerical value 0.

    Secondly, one of Perl's slogan's is There Is More Than One Way To Do It, or TIMTOWTDI as we say. This applies here as well. There is no the correct way, there are many.

    My next question, thus, is: what have you tried? Why didn't it work? Did you get any error messages and if you did, which? How did your output not meet your expectations?

    Lastly, a regular expression in the traditional sense (m//) might not b the most suitable task. What you have is a | delimited string, so most likely you'll want to split on that. Keep in mind that split takes a regular expression as its first argument, and | has a special meaning inside regular expression so you'll have to escape that.

    Then you'll want to iterate over the list you get, using something like map for example, or if you're uncomfortable with those merits of functional programming, you could do it with a simple foreach loop over the array.

    This should give you the building blocks you're looking for.

      Thank you.

      the back ground is I am using regexp in sql (not really perl this time)

      I tried the following:

      REGEXP_SUBSTR({total},'^*', 1, 1) where total is the input string

      I result I am getting is its not recongizing the empty (or null) columns.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2014-08-01 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (27 votes), past polls