Home > Archive > PERL Beginners > February 2007 > extract data from array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
extract data from array
|
|
| Irfan Sayed 2007-02-27, 4:01 am |
| Hi All,
I have following data in the 0 th position of the array.i need to
extract only 143
Days Old:143--User:yevala--Element:/vobs/ivr/air.pj/hdrs/irDefines.h
I did in the following way.
my @data=("Days
Old:143--User:yevala--Element:/vobs/ivr/air.pj/hdrs/irDefines.h"
);
foreach(@data)
{
my @data1=split(':', $_, 1);
print "@data1\n";
}
But i am not getting 143 out of it.
Please help
Regards
Irfan.
| |
| Jeff Pang 2007-02-27, 4:01 am |
| ..
>
>my @data=("Days
>Old:143--User:yevala--Element:/vobs/ivr/air.pj/hdrs/irDefines.h"
> );
>foreach(@data)
>{
>my @data1=split(':', $_, 1);
>print "@data1\n";
>}
>
>But i am not getting 143 out of it.
Hello,
How about using regex to extract it?
$ perl -le '
> $str = "Days Old:143--User:yevala--Element:/vobs/ivr/air.pj/hdrs/irDefines.h";
> $str =~ s/Old:\d+/Old:/;
> print $str;'
Days Old:--User:yevala--Element:/vobs/ivr/air.pj/hdrs/irDefines.h
--
Jeff Pang
EMAIL: pangj<at>earthlink.net AIM: jeffpang
| |
| Paul Lalli 2007-02-27, 7:59 am |
| On Feb 27, 1:14 am, isa...@avaya.com (Irfan Sayed) wrote:
> I have following data in the 0 th position of the array.
Well, yes, but in reality you have the following data in the ONLY
position in the array. So why is it in an array to begin with?
i need to
> extract only 143
> Days Old:143--User:yevala--Element:/vobs/ivr/air.pj/hdrs/irDefines.h
>
> I did in the following way.
>
> my @data=("Days
> Old:143--User:yevala--Element:/vobs/ivr/air.pj/hdrs/irDefines.h"
> );
> foreach(@data)
You're looping over an array that contains exactly one element. That
doesn't make any sense.
> {
> my @data1=split(':', $_, 1);
> print "@data1\n";
>
> }
>
> But i am not getting 143 out of it.
>
What made you think you were going to?
You seem to have drastically misunderstood the documentation for
`perldoc -f split`. Specifying a limit of 1 means that the string
will be broken up into no more than ONE component - that is, it won't
be broken up at all!
I have no idea why you wanted to use split for this, but if you did:
my $num = (split /--|:/, $data[0])[1];
That is, generate a list of all components that are separated by
either -- or : and then take the second element from that list.
In reality, a regexp is a better solution:
my ($num) = $data[0] =~ /:(\d+)/;
You need to read up on regular expressions and the split function:
perldoc perlretut
perldoc perlre
perldoc perlreref
perldoc -f split
Paul Lalli
|
|
|
|
|