Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

match expression from query

by PerlSufi (Friar)
on Sep 19, 2013 at 17:25 UTC ( [id://1054886]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I would like to match an expression from a database query. Here is snippet of my code:
my $mail_date = $ARGV[0]; my $row = 0; my $col = 0; $xWS->write($row,$col++,$_) for @{$sth->{NAME}}; my $name = @{$sth->{NAME}} =~ /\/.*/; while (my $ar = $sth->fetchrow_arrayref) { s{^.*/}{} for @$ar; # this regex thanks to perlmonks ++$row, $col = 0; $xWS->write($row,$col++,$_) for @$ar, $mail_date; } $xWB->close(); print $name;
when I run my script, I get the error:
Applying pattern match (m//) to @array will act on scalar(@array) at ( +my script name)
For now, I was trying to match anything after the forward slash to see if it worked. But in actuality, I want to match for whole word after the forward slash. I think this can be done with \w
So for now, I would like to ask: how do I match an expression from the query? I intend to use the 'name' variable for another part of my script later.
Any insight is greatly appreciated ;)

UPDATE:
I ended up going with Spreadsheet::ParseExcel to get the string that I wanted.

Replies are listed 'Best First'.
Re: match expression from query
by hdb (Monsignor) on Sep 19, 2013 at 18:45 UTC

    The error/warning is caused by this line:

    my $name = @{$sth->{NAME}} =~ /\/.*/;

    and the problem is with @{$sth->{NAME}} and not with the regular expression (yet). What do you get if you say print @{$sth->{NAME}};?

      Hi hdb, thanks for your reply. I actually get the areas that I am 'selecting' in my query language when I print that. I am considering using Spreadsheet::ParseExcel for this task instead
Re: match expression from query
by CountZero (Bishop) on Sep 19, 2013 at 19:12 UTC
    It is not an error but a warning. A regex will only make sense when applied on a scalar, not on an array, so Perl "decided" that you really meant scalar(@{$sth->{NAME}}). It warns you that it has applied this "correction".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      A regex will only make sense when applied on a scalar, not on an array,

      The m//atch operator applies a regex against a scalar

      The s///ubstitution operator applies a regex against a scalar

      The regex is the stuff in between m/ and /

      The m// is the match operator, the regex is the argument to the match operator match('regex')

      Ah. Thank you, CountZero. You're right. Changing it to that in my script did make it run, but no output was given when I ran it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1054886]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-25 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found