Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

non numeric data

by torres09 (Acolyte)
on Jun 24, 2013 at 12:36 UTC ( [id://1040422]=perlquestion: print w/replies, xml ) Need Help??

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

hey

I have written an if code , which should be true only when it has a non numeric data in array cell

if ($array_H[$i] !~ /^[0-9]+$/) { block; }

but somehow if loop block is visited even if data is numeric

Replies are listed 'Best First'.
Re: non numeric data
by derby (Abbot) on Jun 24, 2013 at 12:53 UTC

    I would assume your data is not what you think it is:

    #!/usr/bin/env perl use strict; use warnings; my @array_H = ( 'ABCD', 1234, '1234', ' 1234' ); foreach my $ele ( @array_H ) { if( $ele !~ /^[0-9]+$/ ) { block( $ele ); } } sub block { my $ele = shift; print "Woot -- $ele --\n"; }
    --
    Woot -- ABCD -- Woot -- 1234 --

    -derby

      ya i figured that out , there is a possibility that numbers having - sign are not being blocked so I modified by code . please tell if it is fine or not

      if (($array_H[$i] !~ /^[0-9]+$/) &&($array_H[$i] !~ /^-+$/))

      I am new to perl so please pardon me for obvious mistakes

        Depending on the source of your data, it may have whitespace (especially a newline) at the end.
        Bill
Re: non numeric data
by ramlight (Friar) on Jun 24, 2013 at 13:13 UTC
Re: non numeric data
by blue_cowdawg (Monsignor) on Jun 24, 2013 at 12:53 UTC
        but somehow if loop block is visited even if data is numeric

    First off you are not doing an apples to apples comparison in your conditional. Secondly you are trying to do pattern matching in a dubious fashion. Check out the following:

    $ cat torres09.pl #!/usr/bin/perl -w use strict; while(my $line=<DATA>){ chomp $line; if ( $line =~ m/^\d+$/){ printf "%s\n",$line; } } exit(0); __END__ 0123 0444 A123 $ perl torres09.pl 0123 0444


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: non numeric data
by Happy-the-monk (Canon) on Jun 24, 2013 at 13:37 UTC

    true only when it has a non numeric data

    I'd personally try this first:

    if ($array_H[$i] =~ m/\D/ ) { block; }

    where \D matches any one character that isn't 0..9.

    Now empty strings and all-0-9-stuff pass, everything else enters the block.

    Cheers, Sören

    (hooked on the Perl Programming language)

      /\D/ matches 1.1, -4 and 1000000000000000 (depending on your CPU architecture).

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

        /\D/ matches 1.1, -4 and 1000000000000000 (depending on your CPU architecture).

        Good point, tobyink!

        Cheers, Sören

        (hooked on the Perl Programming language)

Re: non numeric data
by hdb (Monsignor) on Jun 24, 2013 at 12:46 UTC

    Can you provide some examples where you hit the block with numeric data?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found