Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Array Manipulation

by kcott (Chancellor)
on Oct 08, 2013 at 08:32 UTC ( #1057374=note: print w/replies, xml ) Need Help??

in reply to Array Manipulation

G'day Jalcock501,

That's very little to go on. Here's one way to do it:

s/\.fo$// for @files;

Update (in response to your update): You're almost there with s/[.]fo//g. As you can see, is fairly close to what I posted (before you showed any code). Here's the differences:

  • "\." and "[.]" are functionally equivalent: they both match a single '.'.
  • You don't have a '$' anchoring the pattern to the end of the string: without it, the pattern matches ".fo" anywhere in the string.
  • You have a 'g' modifier. This is for repeat matches. You only want to match once, in this instance.

For more details on any of those points, see "perlretut - Perl regular expressions tutorial" and/or "perlre - Perl regular expressions".

-- Ken

Replies are listed 'Best First'.
Re^2: Array Manipulation
by Jalcock501 (Sexton) on Oct 08, 2013 at 08:41 UTC
    Hi Kcott

    There's not a lot to do to be fair, I need to remove the extension because I have files with the same name but different extensions. I'm trying to read them in and compare the different files. So I thought if I remove the extension in the array I can do something like this

    #! /usr/bin/perl -w my @files= <*.in> for (@files) { remove extension } for my $file (@files) { open (IN, "<", "$") || die ("cannot open $"); open (OUT,"<", "$") || die ("cannot open $"); undef $/; my $in = <IN>; my $out = <OUT>; my @in = split /\n/, $in; my @out = split /\n/, $out; my @final; for $a (@in) { my @result = grep/^\Q$a\E$/, @out; push (@final , @result); } print "Search string that matches against general data:\t@final"; }
    I hope this makes a little more sense.
      "There's not a lot to do to be fair, ..."

      My comment, "That's very little to go on.", was written before you updated your OP and added code. I updated my initial response after you added code to your OP.

      Based on the limited information you initially provided, by first response involved a certain amount of guesswork. Once you had provided some code, I was able to provide a much better answer: including pointers to where you went wrong and references to supporting documentation.

      So, had you provided your code initially, you wouldn't have needed to update your post or respond to my post (except, perhaps, to say thankyou). Similarly, I would only have needed to post once, instead of three times.

      There are distinct benefits to following the guidelines in "How do I post a question effectively?". Hopefully, this exercise has highlighted that.

      -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057374]
NodeReaper puts the hammer down

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2018-03-19 01:37 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (231 votes). Check out past polls.