codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Array manipulations  ChonJi at 13:45 on Monday, May 29, 2006
 

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.

  Re: Array manipulations  sdslrn123 at 09:59 on Tuesday, June 06, 2006
 

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.


  Re: Array manipulations  sdslrn123 at 09:59 on Tuesday, June 06, 2006
 

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.


  Re: Array manipulations  Michaels PerlServices at 14:06 on Sunday, July 09, 2006
 

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)








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Insert Contents of .txt file into a .html page
•  Re: link spider
•  Re: strange Perl loop
•  Re: need help writing a simple perl script
•  Re: Yahoo Tabbed menu control
•  Pass a variable to open popup window w/o forms
•  Question in C++
•  open multiple files c++
•  Re: Map and explanation


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006