CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
separate elements common to both arrays with a comma (not trailing)

 



regex2012
User

Nov 17, 2015, 9:33 AM

Post #1 of 5 (1278 views)
separate elements common to both arrays with a comma (not trailing) Can't Post

How to remove a comma from elements of an array

Code
open (my $fh1, "< /tmp/list1.txt") or die "Can't open  for read: $!"; 
my @lines;
while (<$fh1>) {
push (@lines, $_);
}
close $fh1;

open (my $fh2, "< /tmp/list2.txt") or die "Can't open for read: $!";
while (<$fh2>) {
my @fields = split(',', $_);
local $" = ',';
my @strings = "@fields[1]\n";
my @strings1 = "@fields[0]";


my @elements = grep { /[@strings]/ } @lines;
print "@strings1";

This script prints elements common to both the list1.txt and list2.txt arrays.

I get numbers 156157 together when I run this. I can get 156,157, if I put a comma here:

Code
my @strings1 = "@fields[0]";

However, then there is a trailing comma. I believe this is coming from "print" as chop won't take it out either. I have tried numerous ways to get the final comma removed, none work. Any ideas?


(This post was edited by regex2012 on Nov 17, 2015, 9:34 AM)


FishMonger
Veteran / Moderator

Nov 17, 2015, 9:58 AM

Post #2 of 5 (1272 views)
Re: [regex2012] separate elements common to both arrays with a comma (not trailing) [In reply to] Can't Post

You should post sample lines from both files.

You should use the Data::Dumper module to dump out each var as it gets assigned to verify it holds what you expect.

Nearly every single line of that code has problems.

Please explain what you think these 2 lines are doing? I doubt that you have the correct understanding.

Code
my @strings =  "@fields[1]\n"; 
my @strings1 = "@fields[0]";



(This post was edited by FishMonger on Nov 17, 2015, 10:02 AM)


Laurent_R
Veteran / Moderator

Nov 18, 2015, 7:40 AM

Post #3 of 5 (1249 views)
Re: [regex2012] separate elements common to both arrays with a comma (not trailing) [In reply to] Can't Post

I agree with everything FishMonger has said and would add that:
- You should post a complete piece of code; what you posted is incomplete and will not even compile;
- You assign something to @elements (and that seems to be the important part), but don't appear to use it anywhere;
- You should format (indent) your code correctly and consistently;
- Adding the "use strict;" and "use warnings;" pragmas near the top of your code would presumably tell you of some of your errors.

Without seeing the full code and the contents of both input files, we can't help you very much. At this point, I can more or less make sense of the first half of your code, but the second half does not make sense.

A slightly cleaner way to write the first half:


Code
use strict; 
use warnings;
open my $fh1, "<", "/tmp/list1.txt" or die "Can't open list1.txt for read: $!"; # saying which file you can't open
# helps the user, especially if you open several files
my @lines;
while (<$fh1>) {
chomp; # probably better to remove trailing line returns
push @lines, $_;
}
close $fh1;

open my $fh2, "<", "/tmp/list2.txt" or die "Can't open list2.txt for read: $!";
while (<$fh2>) {
chomp;
my @fields = split(',', $_);
# ...
}



FishMonger
Veteran / Moderator

Nov 18, 2015, 8:31 AM

Post #4 of 5 (1245 views)
Re: [Laurent_R] separate elements common to both arrays with a comma (not trailing) [In reply to] Can't Post

I'd replace that first while loop with this:

Code
chomp(my @lines = <$fh1>);



(This post was edited by FishMonger on Nov 18, 2015, 8:31 AM)


Laurent_R
Veteran / Moderator

Nov 18, 2015, 11:00 AM

Post #5 of 5 (1237 views)
Re: [FishMonger] separate elements common to both arrays with a comma (not trailing) [In reply to] Can't Post

Yeah, you're right, but I wanted to have it on a separate line for the benefit of including a comment.

Actually, if it comes to populating an array, I would probably slurp the file directly into the array and chomp the array.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives