kevyt has asked for the wisdom of the Perl Monks concerning the following question:

In the last 3 lines of code, how can I loop through the array @zips and put " around each value as it was when I read the file? I can add a few lins and split it into an array. Then for each value of the new array, print the " and then the value, but that is not cool.

I use to work with someone who I think use to do things like this with map and split and I could NEVER figure out how to use map - lol. I know it can be done with a line or 2 but I dont know how. Input looks like this:
"Anawalt","WV","24808","304","54047","McDowell","P", "Anch","AK","99505","907","02020","Anchorage","N", "Ancho","NM","88301","505","35027","Lincoln","A", "Anchor","IL","61720","309","17113","McLean","P", "Anchor","MS","39776","662","28017","Chickasaw","N",
The code reads in the file and removes the quotes and the carriage return.
while (<IN>){ next if (/^#/); chomp; tr/\"//d; y($city,$state,$zip,$area_code,$fips,$county,$pref,$type) = split ( +/\,/); if ( I want this zip code){ . . . } else{ push (@zips,$_); close(ZIPS); } } #### The question is for the print below #### How can I print " around each value foreach (@zips){ print ; }

Replies are listed 'Best First'.
Re: loop through values in a string and print " " around each value.
by GrandFather (Saint) on Jan 12, 2007 at 02:08 UTC

    Do you mean something like:

    print qq{"$_"\n} for @zips;

    DWIM is Perl's answer to Gödel
      Oh my gosh... That's so awesome !!! lol... I need to look up "qq" Thanks a bunch !!!! This is so clean and perfect!

        The following are all the same:
        "..."
        qq{...}
        qq(...)
        qq!...!
        qq!...!
        etc

        qq is just a double-quoted string, without using double quotes.

        qq{"bla"}
        is easier to read than
        "\"bla\""

        oops, this did not work... I need the " " around every line:
        "Leesville,TX,78122,830,48177,Gonzales,P,"
        vs
        "Leesville","TX","78122","830","48177","Gonzales","P",
Re: loop through values in a string and print " " around each value.
by ikegami (Patriarch) on Jan 12, 2007 at 02:21 UTC

    and I could NEVER figure out how to use map

    The usual use of map is

    my @new = map { EXPR } @old;

    It's equivalent to

    my @new; foreach (@old) { push @new, EXPR; }
      Thanks to EVERYONE !!!
Re: loop through values in a string and print " " around each value.
by ikegami (Patriarch) on Jan 12, 2007 at 02:14 UTC

    First of all, your best off using one of the CSV / xSV modules.

    Secondly, what if the string contains "? I'll use Perl-like backslash escaping.

    sub quote { # Use $_ if no arguments are provided. my $s = @_ ? $_[0] : $_; $s =~ s/([\\"])/\\$1/g; return qq{"$s"}; } # Procedural style print quote foreach @zip; print "\n"; # Functional style print map quote, @zips; print "\n";
      Nice. I might be able to use this later in the code. Thanks
Re: loop through values in a string and print " " around each value.
by jwkrahn (Abbot) on Jan 12, 2007 at 02:51 UTC
    If you only want to extract the zip code then there is no need to modify the whole line:
    while (<IN>){ next if /^#/; # extract $zip, leave line unmodified in $_ ( my $zip = ( split /,/ )[ 2 ] ) =~ tr/"//d; if ( I want this zip code){ . . . } else{ push (@zips,$_); close(ZIPS); } }
      Actually, I need the zip and the city. I am saving all of it to print back to the file later but I wont be checking the other values against anything.
      If the city and zip are what I need. I save it and process a few things.
      else, if the zip and city are not what I am looking for, save it to an array.
      After reading the file, print the unused lines back to the file.
      After processing the values that I needed, I will print those lines back to the file with # at the beginning of the line so I wont process them again :)
        This works but I know there is a cooler way to do it :)
        foreach (@zips){ @a = split /,/; foreach (@a){ print "\"" . $_ ."\","; } print "\n"; }
        Output:
        "Leesville","TX","78122","830","48177","Gonzales","P",
        If you only need the city and zip then:
        while (<IN>){ next if /^#/; # extract $zip, leave line unmodified in $_ my ( $city, $zip ) = ( split /,/ )[ 0, 2 ]; tr/"//d for $city, $zip; if ( I want this zip code){ . . . } else{ push (@zips,$_); close(ZIPS); } }
Re: loop through values in a string and print " " around each value.
by siva kumar (Pilgrim) on Jan 12, 2007 at 03:34 UTC
    Try this ..
    $str = "Leesville,TX,78122,830,48177,Gonzales,P,"; $str = "\"$str"; $str =~ s/\,/\"\,\"/g; $str =~ s/\"$//; print $str;
      I dont know why I did not see this... This is a good solution. Save it as a var before I change it. Then push onto an array. Then loop through the array and print it.
      while ( <IN> ){ $str = $_; #### I added this chomp; tr/\"//d; if (/^#/){ ### added this push (@zips,$_); next; } if (I want this location){ . . . } else{ push (@zips,$str); } } # while foreach (@zips){ print ; }