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

use the cut function in a script

by raghu_shekar (Novice)
on May 21, 2009 at 04:46 UTC ( #765377=perlquestion: print w/ replies, xml ) Need Help??
raghu_shekar has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i have an array whose contents are like this "<NumData>1234567890</NumData>" . what i need is just the numeric value without the start and end tags. Is there any cut function in perl which will help me achieve this... It would be great if i could do this to all the elments of the array at one go. What i am trying is to pull each element from the array and perform the cut function to pull just the numeric data and put it into another array.

Comment on use the cut function in a script
Re: use the cut function in a script
by JavaFan (Canon) on May 21, 2009 at 04:58 UTC
    Sounds like:
    my @numbers = map {/([0-9]+)/} @original_array;
    will do the trick.
Re: use the cut function in a script
by Polyglot (Monk) on May 21, 2009 at 05:34 UTC
    Just for the sake of education, here's another regexp method that would focus on those tags:
    foreach $line(@source_array) { $line =~ s/<.*?>//g; push @target_array, $line; }

    Blessings,

    ~Polyglot~

Re: use the cut function in a script
by vinoth.ree (Parson) on May 21, 2009 at 08:01 UTC

    If the tag is same for all the elements, try this also

    use strict; use warnings; use Data::Dumper; my @array=('<NumData>1234</NumData>','<NumData>543535<NumData>'); my @Output; foreach my $data(@array){ $data=~ s/<NumData>|<\/NumData>//g; push @Output, $data; } print Dumper @Output;
    Vinoth,G
Re: use the cut function in a script
by graff (Chancellor) on May 21, 2009 at 11:25 UTC
    The unix "cut" command (which isn't quite suitable here) has a lot in common with the perl "split" function, especially if you use split inside an array slice:
    my @tag_arr = qw(<NumData>1234567</NumData> <NumData>2345678</NumData> +); my @out_arr; for ( @tag_arr ) { push @out_arr, ( split /(\d+)/ )[1]; }
    (I could have used  /[<>]/ as the regex for the split, and then the numeric string would be the third element, but splitting on (and capturing) the digit string seemed simpler somehow.)
Re: use the cut function in a script
by apl (Monsignor) on May 21, 2009 at 11:41 UTC
    You could also generalize your solution and look at one of the CPAN XML modules.
Re: use the cut function in a script
by leslie (Pilgrim) on May 21, 2009 at 11:54 UTC
    Dear firend,
    You can use this bellow code.
    use strict; use warnings; use Data::Dumper; my @parse= qw (<NumData>1234</NumData> <NumData>543535<NumData>); my @parse_out=map{ $1 if (/>(\d+)</) }@parse; print Dumper @parse_out;
Re: use the cut function in a script
by citromatik (Curate) on May 21, 2009 at 12:38 UTC

    There is a nice node from cdarke for equivalences between unix tools/commands and Perl functions. You may find the reading interesting

    citromatik

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-12-21 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls