Perl Array Question

M

mihirtr

Hi,
I am having a huge array that contains file paths in its element.
Following is example of the array
array[0] contains /u/test/abc/foo.c
arroy[1] contains /u/test/abc/foo1.c
array[2] contains /u/test/xyz/abc.c
arroy[3] contains /u/test/xyz/abc1.c
.........

I want to go remove /u/test from each element in the array and store
it. Will slice command help me in doing that?

Thanks
scott.
 
J

Jürgen Exner

Hi,
I am having a huge array that contains file paths in its element.
Following is example of the array
array[0] contains /u/test/abc/foo.c
arroy[1] contains /u/test/abc/foo1.c
array[2] contains /u/test/xyz/abc.c
arroy[3] contains /u/test/xyz/abc1.c
........

I want to go remove /u/test from each element in the array and store
it. Will slice command help me in doing that?

What slice command are your talking about?
C:\>perldoc -f slice
No documentation for perl function `slice' found

Of course there is more than one way to do it, but if you know that the text
to be removed is a constant then a simple s/// or substr() will do quite
nicely.

jue
 
P

Paul Lalli

I am having a huge array that contains file paths in its element.
Following is example of the array
array[0] contains /u/test/abc/foo.c
arroy[1] contains /u/test/abc/foo1.c
array[2] contains /u/test/xyz/abc.c
arroy[3] contains /u/test/xyz/abc1.c
........

I want to go remove /u/test from each element in the array and store
it. Will slice command help me in doing that?

No. Could you please explain what has let you to the belief that
slice() *might* help, so that we can help to clear up your
misperceptions? Thanks.

Loop through each element of the array using a foreach loop. Remove
the offending substring or pattern with substr() or s/// from the
current iteration's element. The aliasing nature of foreach will cause
the array to be updated with the new value of the current element
automatically.

Recommended reading:
perldoc perlsyn
perldoc -f substr
perldoc perlretut

Paul Lalli
 
J

John Bokma

Hi,
I am having a huge array that contains file paths in its element.
Following is example of the array
array[0] contains /u/test/abc/foo.c
arroy[1] contains /u/test/abc/foo1.c
array[2] contains /u/test/xyz/abc.c
arroy[3] contains /u/test/xyz/abc1.c
........

How did they get into the array? Maybe the best option is to make sure
that the information doesn't get stored in the array in the first place?

If the part you want to remove is fixed in length, you might want to check
out perldoc -f substr.
 
U

Uri Guttman

MW> Thus spoke Lawrence Statton XE2/N1GAK (on 2006-12-21 19:41):
MW> This won't work as intended, because
MW> you *are* modifying the aliased $_
MW> in the substitution.

MW> @fixed_array = map { (my $tmp=$_) =~ s|/u/test||; $tmp } @array;

it is much simpler to just grab the part you want.

@fixed_array = map m{^/u/test(.+)$}, @array;

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top