Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Array Manipulation

by kcott (Abbot)
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


Comment on Re: Array Manipulation
Select or Download Code
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, "<", "$file.fo") || die ("cannot open $file.fo"); open (OUT,"<", "$file.bo") || die ("cannot open $file.bo"); 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057374]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-12-26 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls