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

Hi all, I want to read a set of numeric values from a file.
I have a file(response_1.xml), it contains only one line. The line is like:
<>.....</>....<member_id>60</member_id><..><..>...<member_id>74</membe +r_id>....<member_id>83</member_id><>....</>...
I want to read 60, 74 and 83 in an array. I tried to get that, but i am getting only the first value(60). My script:
#! /usr/bin/perl -w $file = '/home/anushya/response_1.xml'; open(FP, $file); @lines = <FP>; foreach $num (@lines) { if ($num =~ m/<member-id>(\d+)/){ @val = $1; print $val[0],"\n"; } } close(FP);
Anybody please help me to solve this.
regards, G. Anushya.

Edited by Chady -- added code tags, and minor formatting.

Replies are listed 'Best First'.
Re: Reading a particular values from the file.
by anniyan (Monk) on Nov 23, 2005 at 14:12 UTC

    anushya, If i understood your question correctly, instead of if you use while

    while ($num =~ m/<member-id>(\d+)/g){ push (@val, $1);} print "@val\n";

    Also you can assign the $1 value to a string $val rather to an array @val.

    while ($num =~ m/<member-id>(\d+)/g){ $val = $1; print $val,"\n";}

    or simply

    @num = $num =~ m/<member-id>(\d+)/g;

    I think this is your first posting, welcome to the Monastery, use <c> tag for coding part.

    updated

    Regards,
    Anniyan
    (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Re: Reading a particular values from the file.
by murugu (Curate) on Nov 23, 2005 at 14:29 UTC
    Hi anushya,

    Best way to handle XML is to use XML modules such as XML::Simple, XML::Twig, etc.....

    Here is the code which will do what you need....

    use strict; use warnings; my $data=<DATA>;#As only one line in the file... my @member_nos = $data=~m/<member_id>(\d+)/g; print join $/,@member_nos; __DATA__ <member_id>60</member_id><a>a</a><b>b</b><member_id>74</member_id><mem +ber_id>83</member_id>

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: Reading a particular values from the file.
by davorg (Chancellor) on Nov 23, 2005 at 14:15 UTC

    (If you want people to help you then a good start would be to format your code so that it is readable.)

    I think you want something like this (untested):

    @val = $num =~ /<member-id>(\d+)/g;

    You are currently only asking for one match, so you only get the first one.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Reading a particular values from the file.
by marto (Cardinal) on Nov 23, 2005 at 14:17 UTC