|
Hello! Need a little help for my project.
I have this array of strings, say some thing like:
x101032
x101822
x101209
x102292
x101302
x102938
x102943
x101289
What I want to do is get a subset of the array containg all the strings that have the substring "102" in them. In this case: the new array would contain
x102292
x102938
x102943
One approach is to use a for/while loop, but is there a much efficient way to do this? thanks.
|
|
|
Hi There
I am a new programmer myself so I am not an expert but I'll try to help.
Hello! Need a little help for my project.
I have this array of strings, say some thing like:
x101032
x101822
x101209
x102292
x101302
x102938
x102943
x101289
What I want to do is get a subset of the array containg all the strings that have the substring "102" in them. In this case: the new array would contain
x102292
x102938
x102943
One approach is to use a for/while loop, but is there a much efficient way to do this? thanks.
Okay if all if the following is an array called @Chonji:
x101032
x101822
x101209
x102292
x101302
x102938
x102943
x101289
and you want to convert it to an array called @Ji containing:
x102292
x102938
x102943
Is this correct?
Am I correct in assuming that you only want to remove the codes that contain x102... (as this is the only unique feature I can see common to all of them.
I could be wrong but in theory you could say:
@Chonji = ('x101032', 'x101822', 'x101209', 'x102292', 'x101302', 'x102938', 'x102943', 'x101289')
foreach $element @Chonji{
if ($element =~ /x102/){
print $element;
}
}
#This would print the required results.
#Now let me see to print to an array. You could use the push function.
#!/usr/bin/perl -w
use strict;
my $element;
my @Ji;
my @Chonji = ('x101032', 'x101822', 'x101209', 'x102292', 'x101302', 'x102938', 'x102943', 'x101289');
foreach $element (@Chonji){
if ($element =~ /x102/){
push (@Ji, $element);
print "$Ji[\n";
}
}
You need to grab the third line...
I don't think I've answered your question but let me know how you get on.
|
|
|
Hi There
I am a new programmer myself so I am not an expert but I'll try to help.
Hello! Need a little help for my project.
I have this array of strings, say some thing like:
x101032
x101822
x101209
x102292
x101302
x102938
x102943
x101289
What I want to do is get a subset of the array containg all the strings that have the substring "102" in them. In this case: the new array would contain
x102292
x102938
x102943
One approach is to use a for/while loop, but is there a much efficient way to do this? thanks.
Okay if all if the following is an array called @Chonji:
x101032
x101822
x101209
x102292
x101302
x102938
x102943
x101289
and you want to convert it to an array called @Ji containing:
x102292
x102938
x102943
Is this correct?
Am I correct in assuming that you only want to remove the codes that contain x102... (as this is the only unique feature I can see common to all of them.
I could be wrong but in theory you could say:
@Chonji = ('x101032', 'x101822', 'x101209', 'x102292', 'x101302', 'x102938', 'x102943', 'x101289')
foreach $element @Chonji{
if ($element =~ /x102/){
print $element;
}
}
#This would print the required results.
#Now let me see to print to an array. You could use the push function.
#!/usr/bin/perl -w
use strict;
my $element;
my @Ji;
my @Chonji = ('x101032', 'x101822', 'x101209', 'x102292', 'x101302', 'x102938', 'x102943', 'x101289');
foreach $element (@Chonji){
if ($element =~ /x102/){
push (@Ji, $element);
print "$Ji[\n";
}
}
You need to grab the third line...
I don't think I've answered your question but let me know how you get on.
|
|
|
From: Michael (http://Perl-services.mprv.biz)
Hello ChonJi,
You can try this:
[code]
@x = ('x101032', 'x101822', 'x101209', 'x102292', 'x101302', 'x102938', 'x102943', 'x101289');
@y = grep /102/, @x;
[/code]
@y will contain
0 'x102292'
1 'x102938'
2 'x102943'
Hope this helps.
Kind regards, Michael
(Michaels Perl-services)
|
|
|
|
|
|
|
|