in reply to To Split or Not to Split

Add "perldoc" to your google search term.

http://perldoc.perl.org/functions/split.html has plenty of examples to try.

Also, keep a test.pl file around, so you can try things out and see what happens. Very handy.

Replies are listed 'Best First'.
Re^2: To Split or Not to Split
by burningredmoon (Novice) on Apr 24, 2011 at 22:34 UTC
    Thank you for replying! I assume that I would be needing to use this part in the link you sent me:
    open(PASSWD, '/etc/passwd'); while (<PASSWD>) { chomp; ($login, $passwd, $uid, $gid,$gcos, $home, $shell) = split(/:/); #... }

    What exactly is the first line doing? The while is reading through the file at the directory /etc/passwd ?

    For my purposes would I write

    open READINGFILE, "data.txt" or die $!; open LOGFILE, ">>logfile.txt" or die $!; while (<READINGFILE>) { chomp; ($domain, $IP) = split(/,/); if ($ip_obj->{src_ip} eq $IP){ print LOGFILE $domain; print LOGFILE " has been found"; } }

    Right now I'm getting errors:

    Global symbol "$domain" requires explicit package name at test.pl line + 113. Global symbol "$IP" requires explicit package name at test.pl line 113 +. Global symbol "$IP" requires explicit package name at test.pl line 114 +. Global symbol "$domain" requires explicit package name at test.pl line + 115. BEGIN not safe after errors--compilation aborted at test.pl line 235 ( +#1) (F) You've said "use strict" or "use strict vars", which indicates that all variables must either be lexically scoped (using "my" or +"state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::"). Uncaught exception from user code: Global symbol "$domain" requires explicit package name at test +.pl line 1 13. Global symbol "$IP" requires explicit package name at test.pl line 113 +. Global symbol "$IP" requires explicit package name at test.pl line 114 +. Global symbol "$domain" requires explicit package name at test.pl line + 115. BEGIN not safe after errors--compilation aborted at test.pl line 235. at test.pl line 235

    What am I missing?

    And yes I have several files at various steps in the process to make sure I don't take steps backwards, thanks for the advice though and the link is a good reference but I need it a little more... dumbed down for me to understand. lol.

      Here's the list of tips

      • 1) Lexical file handles: my $fh
      • 2) 3 parameter form of open
      • 3) Validate your data file to make sure each line has data.
      • 4) Declare your variables inside the loop with my
      open my $data_fh, 'data.txt' or die $!; open my $log_fh, '>>', 'logfile.txt' or die $!; while (<$data_fh>) { chomp; next if /^\s*$/; my ($domain, $IP) = split ','; if ($ip_obj->{src_ip} eq $IP){ print $log_fh "$domain has been found"; } }
        Thank you for replying with this! This is what I was needing!!!! It works awesomely, thank you so much!!!!

      It is opening a file called 'password' (no extension)

      "'X' requires explicit package name" means you haven't declared those variables (or you typoed them). That's what the text between the "(F)" and the "uncaught exception" is basically saying.

      Use: my ($domain, $ip) = split(/,/); to declare them while you assign the values.

      Note: the test.pl isn't for backups. It is for trying out new things to see what they will do. Use it to play around with split or regexes or whatever you're not sure about. Its all about having a tiny, clean sandbox to try things out in, where you know there is no other code to confuse you.

        Oh ok, I'll try that. Thank you