in reply to Extract data from CSV field.

I'm guessing you want to extract just the #number part from a string.

#!perl use strict; my @data = ( "TRAY HINGED PLSTC 20 CAV #F32473", "BOX HSC 35-3/4X17-1/4 X 50-1/2 SIMULATOR TALL BOX", "PAD FOAM 24 X 24 X 1/4 #16193 112 SHEETS PER ROLL ORDER IN FULL ROLLS +", "PKG LIST ASST ARM RAD 300 #F37784", "PAD TOP CAP RE17-30048 #F30121 CORRUGATED ASSEMBLY 22-7/8 X 21-1/8 X +4-3/4"); for my $str (@data){ if ($str =~ /(#[^ ]+)/){ print "'$1'\n"; } else { print "No match\n"; } }
poj

Replies are listed 'Best First'.
Re^2: Extract data from CSV field.
by JobC (Acolyte) on Dec 08, 2015 at 23:49 UTC

    ww and stevieb I think poj has what I am looking to do. I was being thrown on how to handle the "#" since it is a comment operator. I thought I had to escape it. I'll test this and see if it solves my problem. Thanks!

      This snippet correctly identifies the section of the CSV I want to extract. But I am doing perl wrong. I keep getting a "true" or "1" in my test output.

      my $var = $row->[5] =~ /(#[^ ]+)/;

      I can do a replace with this code and overwrite the data I want to keep, I would bet I could use the "not" operator and replace only the other data, but I haven't tried it, and that may not be desirable for reporting purposes later on. How do I assign this "found" value to a new field in the modified dataset? I think I am missing something in my understanding of the syntax.

        To further expand on poj's response, this is a case of calling context. You called the match (and capture) in scalar context, so the pattern match returned the number of matches (which would be at most 1, since you didn't use the g modifier on your RE). poj's addition of the parenthesis around the variable assignment changes the context to 'array context'. The results of a capture in scalar context return the number of matches, and in array context return the matches themselves.

        --MidLifeXis

        my ($var) = $row->[5] =~ /(#[^ ]+)/ ;
        poj